Using ELMAH with BlogEngine.NET
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.
<configuration> <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"/> <section name="errorTweet" requirePermission="false" type="Elmah.ErrorTweetSectionHandler, Elmah"/> </sectionGroup> </configSections> <elmah> <security allowRemoteAccess="yes" /> <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="C:\inetpub\wwwroot\blog\app_data\elmah" /> </elmah> <system.web> <httpModules> <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/> </httpModules> <httpHandlers> <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" /> </httpHandlers> </system.web> <!-- IIS 7 --> <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <modules> <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" /> </modules> <handlers> <add name="Elmah" path="elmah.axd" verb="POST,GET,HEAD" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" /> </handlers> </system.webServer> <location path="elmah.axd"> <system.web> <authorization> <deny users="?"/> </authorization> </system.web> </location> </configuration>
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).