Friday, August 7, 2009

ASP.NET Error Logging with ELMAH

Debugging production code is always a challenge. From my experience I have used the Windows event logs and the Apache log4net so far.

Most of the time log4net is the solution for all my needs and I like how flexible it is. But they were situations where the logging solution was not enough. The real problem occurs if in your application you get unhandled exceptions. You know - database connection broken, network downtime, system software updates - it's in many ways possible to throw the application into confusion without having even the near idea where to look for. And this is where ELMAH comes to the rescue.

You can include ELMAH in your projects with a little effort and even with the default configuration you take benefits. ELMAH can log errors for you to different places - from xml files to database servers but the easiest way is to just store error messages into memory. ELMAH will give you a way to check for unhandled exceptions from special URL on your ASP.NET site.

Addiding ELMAH to a project



Having heard for ELMAH from Hanselminutes I decided to give a try.
There was a project under active development where new versions uploaded on the staging server very frequently, even many times a day.

After the download of the binaries I just added a reference to Elmah.dll into the project. Then following the instructions on the ELMAH documentation I made few additions to the Web.config file.

a/ I added to "configSections":


<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>


b/ Then added into "httpHandlers":

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


c/ and at the end into "httpModules":

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




I also added another configuration option to enable opening the ELMAH views from remote connection, not only on the server, into section "configuration" I added:


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


That was everything necessary to include ELMAH to the project.



Did I say - ELMAH is wonderful!



Having ELMAH included to the web application with this configuration, we have got the error logging information accessible at http://APPHOME/elmah.axd.

We put into the staging server only well unit-tested code. Once as we did changes on the database schema we have forgotten to patch the database where the staging server connects.

During the integration tests the application raised an exception. On the browser the exception information was not very helpful (see: yellow screen of dead). Into the log files generated by log4net there was nothing mentioned. Then we opened the ELMAH page and we saw the reason for the exception:




ELMAH is a tool to help you with any ASP.NET project. Use it right now.

No comments:

Post a Comment