Set It and Forget It
By Jonathan Goodyear
If you ve watched any amount of television here in the United States lately, you ve likely seen the plethora of car insurance commercials. While some of them are amusing to watch, it always surprises me how they try to make the topic of car insurance seem like a central theme to the average person s life. While just about everybody who owns a car also has car insurance (it s the law in most places), it s just not something that most people spend much time thinking about. You do a little bit of research, pick a provider that meets your needs, and you re done. I have personally had the same car insurance provider for the last decade. I recall the famous slogan coined by the infomercial king Ron Popeil of Ronco, Set It and Forget It (http://en.wikipedia.org/wiki/Ronco). That summarizes my feelings about car insurance quite nicely.
All of that said, you put your car insurance in place for a reason. When the unfortunate (some would say inevitable) accident occurs, you want your car insurance provider to spring into action and do its job to take care of things. I like programming tools that work the same way. I can take the time to set them up once, then they ll get out of my way until I need them at which point, they ll perform their job exactly as I intended. In fact, keeping with the theme of accidents, there is a free tool at your disposal that won t get in your way and can handle programmatic accidents (exceptions) quite gracefully.
The tool to which I am referring is ELMAH (Error Logging Modules and Handlers; http://code.google.com/p/elmah/). At its core, ELMAH is a pluggable exception management and logging engine for ASP.NET. It is implemented as a set of HttpModules and HttpHandlers that you can drop into any existing ASP.NET application to manage and log all unhandled exceptions. I particularly like the fact that it doesn t require you to change any of your code to make it work. Drop an assembly into your /bin directory and add a couple of lines of code to your web.config file (to let your ASP.NET application know that ELMAH is in the house) and you re good to go. You can even add the ELMAH assemblies to the Global Assembly Cache (GAC) and instantly get exception handling and logging for all the ASP.NET applications on an entire Web server.
Note that there are a couple of extra entries that you ll need to add to your machine.config file to enable this option. If you have an ASP.NET application and don t yet have a cohesive strategy to account for the unexpected, I highly recommend ELMAH. You ll especially appreciate the fact that you can access a log of the exceptions generated by your application via a Web page or RSS feed. If you want a more direct approach, you can even have each exception e-mailed to you.
Some of you may be wondering why I haven t mentioned the Exception Handling Application Block, which is one of the pieces of the Enterprise Library suite created by Microsoft s Patterns & Practices team (http://msdn2.microsoft.com/en-us/library/aa480461.aspx). In fact, I do like the Exception Handling Application Block. Besides basic exception handling and logging, it also includes the ability to set policies for different types of exceptions or exceptions that occur in different parts (tiers) of your application, as well as enabling you to wrap exceptions. This provides a great security barrier when interfacing with external applications through interfaces such as Web services.
However, the Exception Handling Application Block can be a bit of overkill for most ASP.NET applications. While there are multiple logging options, it prefers to send events to the Windows Event Log, which requires special permissions that ASP.NET does not have by default. It also requires more code to implement. Overall, it s a more powerful solution; my company uses it for several of our clients for whom we are building applications that utilize Windows Presentation Foundation (WPF) and Windows Communication Foundation (WCF). In those environments, the Exception Handling Application Block s additional flexibility is worth the extra work to implement it. For most ASP.NET applications, though, I prefer to Set It and Forget It . For that, ELMAH truly shines.
Jonathan Goodyear is president of ASPSOFT (http://www.aspsoft.com), an Internet consulting firm based in Orlando, FL. Jonathan is Microsoft Regional Director for Florida, an ASP.NET MVP, a Microsoft Certified Solution Developer (MCSD), and co-author of ASP.NET 2.0 MVP Hacks (Wrox). Jonathan also is a contributing editor for asp.netPRO. E-mail him at mailto:[email protected] or through his angryCoder eZine at http://www.angryCoder.com.