ELMAH is a library you can drop into any ASP.Net WebForms or MVC applicaiton and automatically capture and record every unhandled exception in your application. User get a 404 looking for a page that doesn’t exist? Recorded. Try to access a member of a null object? Recorded. Database query throw an error caused by gremlins? Recorded.
There are other sources to tout how great ELMAH is (Scott Hansleman’s initial piece and again when NuGet was released) so I won’t go into that here. This post is about the minimum steps to getting ELMAH running on ASP.Net MVC.
ELMAH is pretty well documented, in fact it was initially just supposed to be sample code demonstrating ASP.Net features for an MSDN Article, however, however integration with MVC is sorely lacking in the official docs. In tradational ASP.Net you would set customErrors off in web.config to show a custom page after an error instead of the Yellow Screen of Death.
The crux of the problem is MVC uses a global action filter named HandleErrorAttribute for the same purpose. But because HandleErrorAttribute takes care of the error and never passes it along to ELMAH. Oddly, the “official” solution to this is a question on StackOverflow. Essentially the suggested solution from the author of ELMAH is to inherit HandleErrorAttribute and override the OnException member so that the exception is passed to ELMAH.
But it seems odd that this wouldn’t be a core part of the ELMAH package given how many people want to use it on MVC. Shouldn’t this be turned into a DLL that can be dropped into a project like ELMAH? It should. And it has.
The ELMAH.MVC NuGet package by Alexander Beletsky takes care of the HandleErrorAttribute and even adds some configurable authorization to the ELMAH controller. It’s all nicely documented on the projects GitHub page. Simply install the NuGet package, delete any existing HandleError filter you have and you’ve got ELMAH working in MVC.
(One quick note about authorization in ELMAH.MVC. If you look at the source code you’ll see an [Authorize] attribute on the controller. This isn’t the core ASP.Net attribute, it’s a custom ELMAH.MVC attribute that works with the web.config settings.)
However there was one configuration I missed the first time around. I had ELMAH deployed to a test environment and hit an error. Thinking I was so smart for having taken the 90 seconds to install ELMAH I went to https://myapp/ELMAH only to receive a 403 authentication error. I hadn’t configured the ELMAH.MVC authorization yet so I wasn’t sure what was going on. Only after a little googling did I find my missing configuration, allowing remote access.
<security allowRemoteAccess="1" />
If you do allow remote access, make sure you also turn on the authorization.