9

I want email to be sent only on a specific condition and log error in DB in all cases. But as I understand, filtering can't work for one of the two. Is that right? If so then how can I achieve it?

Also to note that, right now I'm saving additional info to database on ErrorMail_Mailing in global.asax as replied by Atif Aziz. Because email will be sent only on conditional basis and ErrorMail_Mailing fires only while sending email, I wonder how would I be able to save additional info of all errors to database.

UPDATE:
I have modified Elmah code a bit to satisfy my need.

Community
  • 1
  • 1
IsmailS
  • 10,797
  • 21
  • 82
  • 134

4 Answers4

2

The first step is to configure modules. Make sure you add Elmah.ErrorFilterModule after any of the logging modules from ELMAH, as shown here with ErrorLogModule:

<httpModules>
    ...     
    //email
    <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
    //sql
    <add name="ErrorSql" type="Elmah.SqlErrorLog, Elmah"/>
    <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>

    ...
</httpModules>

Then in your configuration section registered Elmah.ErrorFilterSectionHandler as shown here:

<configSections>
    <configSections>
      <sectionGroup name="elmah">
          <section name="errorFilter" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
      </sectionGroup>
  </configSections>

Now you can add filters to decide what errors to be ignored for what source. The following example shows how to prevent having 404 HTTP errors being mailed.

<elmah>
    <errorMail from="xx@xx.com" fromName="xx" to="xx@xx.com" subject="An unhandled exception occured xxx" priority="Normal" async="false" smtpServer="xx.xx.xx.com"/>
    //sql
    <errorLog name="ErrorSql" type="Elmah.SqlErrorLog, Elmah" connectionStringName="MyConnectionString" />
    <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
    <errorFilter>
        <test>
            <and>
                <equal binding="HttpStatusCode" value="404" type="Int32" />
                <regex binding="FilterSourceType.Name" pattern="mail" />
            </and>
        </test>
    </errorFilter>
</elmah>  

You can find out more detail information on the following link.

http://code.google.com/p/elmah/wiki/ErrorFiltering

Jack
  • 2,600
  • 23
  • 29
1

The ELMAH documentation on error filtering has a section on exactly your scenario and is called, which amounts to filtering by source. For example, the following will prevent 404 HTTP errors from being mailed but they will be still logged (assuming both mailing and logging modules are registered):

<errorFilter>
    <test>
        <and>
            <equal binding="HttpStatusCode" value="404" type="Int32" />
            <regex binding="FilterSourceType.Name" pattern="mail" />
        </and>
    </test>
</errorFilter>
Atif Aziz
  • 36,108
  • 16
  • 64
  • 74
0

You should try out the StackExchange.Exceptional

This project was inspired by ELMAH, but it didn't suit our particular needs for very, very high volume error logging when a network-level event occurs.

StackExchange.Exceptional is the error handler used internally by Stack Exchange and Stack Overflow for logging to SQL.

It also supports JSON and memory error stores, filtering of exceptions before logging, and fail/retry mechanisms for storing errors if there's an interruption in connecting to the error store.

It's highly customizable and it's really easy to add something according your needs. As i can see an pull request has the email functionality implemented Email functionality so you can start from there.

To set it up you only need to look at the web.config and pick what to enable.

Hope it helps.

Matija Grcic
  • 12,963
  • 6
  • 62
  • 90
0

If you want to save all exceptions to the database you should just be able to use the ErrorLogModule like so which should be independent of what you are doing in the error mail module:

<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />

and then in your elmah secition of your config:

<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="MyConnectionString" />
Drew Freyling
  • 1,258
  • 13
  • 14
  • Then what about emailing? Right now I've taken the source code of ELMAH and modifying it for my purpose. But it is taking time. – IsmailS Mar 05 '12 at 04:38