Logging Errors with ELMAH



ELMAH (Error Logging Modules and Handlers) is a pluggable component that you can use to log errors without modifying the application code. The main advantage of ELMAH is it's pluggable feature. You can easily integrate the ELMAH component in your developed application. It's an open source project and you can customize the code according to your needs. I am not going to show you how to customize the component; instead configuration of this component with your ASP.Net application and how you can store the error messages in SQL Server. You can also store the error information in Oracle, Access or XML. There are several options available in their website.

First task is to download the ELMAH project from following link

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

Once download is complete, create one website and add the elmah.dll reference from bin folder under the downloaded folder. All versions dll are available starting from .Net fx 1.1 to .Net fx 3.5.

1.gif

Next, make the appropriate ELMAH entries in Web.config. In ConfigSections, add the following:

<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah"/>
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah"/>
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
</sectionGroup>

2.gif

Add this top level element:

<elmah>
<security allowRemoteAccess="yes"/>
</elmah>

3.gif

Under httpModules add this line:

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

3.5.gif

Under httpHandlers, add:

<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah"/>

4.gif

Finally, add this top level element:

<location path="elmah.axd">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>

5.gif

That's all. You completed the configuration to start the ELMAH.

You can now see the elmah.axd file. Open your browser and type http://localhost:%3Cport%3E/elmah.axd

6.gif

You will now create some exception intentionally like divide by zero, calling the page which does not exist etc.

Create a webform and put the following code in page_load section.

int temp = 0;
Response.Write(Convert.ToString(125 / temp));


Run the page and you will see the "yellow death screen" page.

Again run the elmah.axd page and you will find the exception details logged in the page. Try to access the page which doesn't exist in application and the exception logged in elmah.axd page. So any application exception will log and display in elmah.axd file.

Untill this point, I did not change any application specific code. I wrote some configuration in web.config and used the elmah.dll and my exception logging starts. That is the main beauty of ELMAH.

Moving next, I will show you how to log this exception details in SQL Server database. The first task is to create the tables in the database. Open the downloaded ELMAH folder and locate the SQLServer scripts under db folder and run against your database. The ELMAH_Error table and stored procedures will be created.

7.gif

Add a connection string element in web.config to point to the database.

<connectionStrings>
    <
add name="ElmahConnectionString" connectionString="Data Source=.\sqlexpress;Initial Catalog=EFBlogs;Integrated Security=True;Pooling=False"/>
  </connectionStrings>

Add following code in web.config under top level <elmah> tag.

<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ElmahConnectionString"/>
That's it. Run the pages that will raise exception and you will see the exception details starts logging in ELMAH_Error table.
  

Up Next
    Ebook Download
    View all
    Learn
    View all