Getting started with ELMAH: ASP.NET Error Logging and Reporting
This is the first part of a series of posts showing you how to use ELMAH to handle error logging for your ASP.NET web applications.
What is ELMAH?
From the official site: “ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.”
Why should I care?
Once you have dropped the ELMAH dll into your bin directory and added a couple of definitions to your web.config you get a bunch of seriously cool features without changing a single line of code:
- Logging of nearly all unhandled exceptions.
- Remotely view the log of exceptions and full details of any one exception, including the ability to see the original YSoD.
- Options to receive an e-mail notification of each error at the time it occurs, an RSS feed of the last 15 errors from the log or a Tweet of the error.
Sounds good! How do I get started?
Head over to the ELMAH downloads page and grab the binaries. Make sure you pick the right download, x86 v x64 depending on your platform. Drop the Elmah.dll file into your web sites /bin directory.
At this point you have a choice to make. You need to pick a storage medium to hold your error log and ELMAH has quite a selection to pick from: SQL Server, SQLite, MS Access, Oracle, Vista DB, MySQL.
I’m going to give an example of how to configure SQLite as it’s my preferred storage medium (I’ll explain why in a minute) – for details of how to configure many of the other databases I suggest you check out the Elmah WebBase.
So, why do I use SQLite? Well,
- It’s fast. Very fast. The database is stored in memory and written to a local file in the App_Data directory.
- There’s no client-server traffic to a database server clogging up my network.
- It’s incredibly simple to set up.
To use SQLite we will need to drop the System.Data.SQLite.dll from the ELMAH zip file into the /bin directory. Now we have everything we need, lets configure web.config.
Configuring web.config for ELMAH with SQLite
Below is a minimal web.config stripped of everything but the pieces you need to add to get ELMAH up and running.
<configuration> <configSections> <sectionGroup name="elmah"> <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah"/> </sectionGroup> </configSections> <elmah> <errorLog type="Elmah.SQLiteErrorLog, Elmah" connectionStringName="ELMAH.SQLite"/> </elmah> <connectionStrings> <add name="ELMAH.SQLite" connectionString="Data Source=|DataDirectory|errors.s3db"/> </connectionStrings> <system.web> <httpModules> <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/> <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/> </httpModules> <httpHandlers> <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah"/> </httpHandlers> </system.web> </configuration>
Not a lot to it, eh? Once you’ve added those bits to your config file you application will begin automatically logging your errors and provide you with an interface to view the logged errors via elmah.axd. However, there is much, much more you can do with ELMAH to make your development and maintenance processes slick and efficient.
Getting the most from ELMAH