Few days ago I’ve noticed that “error.aspx” becomes quite popular destination on my site. What’s going on? I never run into errors, how do I know what others do to break in? Elmah to the rescue! This little utility specifically designed to run in the background and record any ASP.NET errors so you can review them later at your convenience. If you interested how it all works, check out this excellent article on MSDN, it goes in depth explaining technical details. I’ll focus here on how to set it up with BlogEngine (or any ASP.NET web forms application to that matter).
First, go get latest binaries from the project site. Unzip and move Elmah.dll to “bin” folder on your site. There are several versions included with download, for BE 1.5 I’m using Elmah\bin\net-2.0\Release\Elmah.dll.
Once you have DLL in the “bin” folder, you need to set up your application to use it. With download comes sample site, it has web.config with examples on how to use Elmah with different configurations (Elmah\samples\web.config). You can set it up to record errors to XML files or number of databases, including SQL server, VistaDB etc. I’m using XML data provider, so I’ll set it up to dump all errors to ~/app_data/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"/>
<section name="errorTweet" requirePermission="false" type="Elmah.ErrorTweetSectionHandler, Elmah"/>
<security allowRemoteAccess="yes" />
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="C:\inetpub\wwwroot\blog\app_data\elmah" />
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
<!-- IIS 7 -->
<validation validateIntegratedModeConfiguration="false" />
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
<add name="Elmah" path="elmah.axd" verb="POST,GET,HEAD" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
As you can see, there are just few things you need to add to configuration to get Elmah started. With this in place, you’ll be able navigate to elmah.axd and see list of errors happened within your application and recorded by Elmah. This includes yellow screen of death and all server variables at the moment error occurred. Very helpful! In my case, it turned out that empty user profile caused Foaf handler to throw an error. Added a null check to condition, and 99% errors went away, no more worries.
If you find it useful, you can extend Elmah to be much smarter, save errors to database, purge after period of time, send email notifications and even twit your errors to the world (no, I’m not trying to be funny – it really has twitter provider).