Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log all error in DB but email errors only on conditional basis?

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.

like image 526
IsmailS Avatar asked Feb 28 '12 07:02

IsmailS


People also ask

How do you automate SQL query and send the result by email?

To send email with T-SQL, you need to use the sp_send_dbmail stored procedure in the msdb database. This procedure accepts many arguments, one of which is the @query argument. That's the argument that attaches the results of your query to the email.

Which technique is suitable for detecting database error in the program?

Finding SQL Injection A response of the server which includes a database error or that is an HTTP error code usually eases the identification of the existence of an SQL injection vulnerability. However, blind SQL injection is something that can also be exploited, even if the application doesn't return an obvious error.

Which SQL clause can be used to match a condition on?

The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified condition.


2 Answers

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="[email protected]" fromName="xx" to="[email protected]" 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

like image 189
Jack Avatar answered Sep 26 '22 21:09

Jack


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>
like image 41
Atif Aziz Avatar answered Sep 25 '22 21:09

Atif Aziz