Elmah - Installation and Setup
As promised, here is my next article regarding another tool I find completely invaluable in my life as a developer, Elmah.
Basically Elmah sites quietly on your site, logging any exceptions (Code based or Web Server, for example, 404) which occur to (in this example) a database. It then provides a nice neat GUI front end to allow you to view the details of these errors, including stack traces.
If you're anything like me, and are tired of conversations which go like this:
User: "Karl, the website crashed earlier"
Karl: "Oh right, what were you doing"
User: "I don't remember, I was just on it, can you fix it please"
Karl: "Well I could do with reproducing it...
You will be happy Elmah exists!
Installation
Installation is easy, just follow these steps. Remember, this guide is based around .NET 4.5 MVC 4 and the associated caveats, it may not be exactly what is required for your configuration but should be near as dammit.
Ok, so firstly, install the Elmah package from the Package Manager console in Visual Studio console with Install-Package elmah
.
We use the package manager as it will handle the installation of any dependencies, and also add the required sections to your web.config (well, almost).
Now open your web.config and find the section. You'll need to modify it to use SQL logging. Make that section look something like this:
<elmah>
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="YourConnectionStringName" applicationName="YourWebsiteName" />
<security allowRemoteAccess="true" />
</elmah>
It's pretty self explanatory, the connection string name must be a valid connection string from the <connectionStrings>
section of your web.config, and the application name should be the name you want to log errors for this application against
.
As you can see in point #2, I have set allowRemoteAccess
to true, this is a personal preference, but if you do this you must secure it. If you use forms authentication with roles this is simple to do, find the <location path="elmah.axd">
element, and make it look something like this:
<location path="elmah.axd">
<system.web>
<httpHandlers>
<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</httpHandlers>
<authorization>
<allow roles="Admin"/>
<deny users="*"/>
</authorization>
</system.web>
<system.webServer>
<handlers>
<add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
</handlers>
</system.webServer>
</location>
You'll notice here I have added a constraint which means only users which are members of the Admin group will be able to view Elmah.axd
The next section you need to modify is in your <system.webServer>
section, you need to add runAllManagedModulesForAllRequests="true"
to the modules section. Without this, elmah will not log exceptions in .NET MVC, you will simply find there is nothing logging to your SQL server:
<modules runAllManagedModulesForAllRequests="true">
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
</modules>
CUSTOM ERRORS
When you use Elmah with Custom Errors redirects, you'll notice that nothing gets logged to elmah. This is because the framework intercepts the error prior to elmah getting hold of it. To get around this we need to add two global filters, the code for them is fairly, simple:
Public Class ElmahHTTPErrorAttribute
Inherits System.Web.Http.Filters.ExceptionFilterAttribute
Public Overrides Sub OnException(actionExecutedContext As System.Web.Http.Filters.HttpActionExecutedContext)
If actionExecutedContext.Exception IsNot Nothing Then
Elmah.ErrorSignal.FromCurrentContext().Raise(actionExecutedContext.Exception)
End If
MyBase.OnException(actionExecutedContext)
End Sub
End Class
And
Public Class ElmahMVCErrorAttribute
Implements IExceptionFilter
Public Sub OnException(filterContext As ExceptionContext) Implements IExceptionFilter.OnException
If filterContext.Exception IsNot Nothing Then
Elmah.ErrorSignal.FromCurrentContext().Raise(filterContext.Exception)
End If
End Sub
End Class
And then you need to modify your FilterConfig.vb
. What we're doing here is creating a new section for HTTP filter attributes, which we will call from global.asax
later:
Public Class FilterConfig
Public Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
'Add elmah attribute
filters.Add(New ElmahMVCErrorAttribute, 1)
'Add the standing handle error attribute
filters.Add(New HandleErrorAttribute(), 2)
End Sub
Public Shared Sub RegisterHTTPFilters(ByVal filters As System.Web.Http.Filters.HttpFilterCollection)
'Add a http filter attribute
filters.Add(New ElmahHTTPErrorAttribute)
End Sub
End Class
Finally, update Application_Start in your Global.asax.vb
to register the new filters:
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)
FilterConfig.RegisterHTTPFilters(GlobalConfiguration.Configuration.Filters)
And that's it, you're all configured and ready to go, if you go to http://yoursite.com/Elmah.axd
, you should see a screen similar to the following:
You can drill down further, by clicking details, which will show you the stack trace if one is available.