Saturday, February 4, 2012

Adding ELMAH to an AppHarbor application

For those who haven't heard of ELMAH yet, here is the project description.
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.
While ELMAH is completely not AppHarbor specific, there do seem to be a fair amount of questions on the AppHarbor support forums on how to get ELMAH running. I just installed and configured ELMAH for one of my AppHarbor ASP.NET MVC3 applications, so I thought it would be nice to share and give something back to the AppHarbor support guys.

I think enabling ELMAH shouldn't take more than 10 minutes if you follow this tutorial.


The easiest way to add ELMAH to your web application is by installing the nuget package.
PM> Install-Package elmah
Attempting to resolve dependency 'elmah.corelibrary (= 1.2)'.
Successfully installed 'elmah'.
Successfully added 'elmah' to Docary.
Once this is done you will find a newly added Elmah reference and a changed web.config.

I'm not going to list all the changes to the web.config here. If you're interested in what the package does to your web.config, you should just run a git diff.

Error log storage

You have to tell ELMAH where it should store the logs. A list of error log implementations can be found on the project wiki.

I choose to use the in-memory error log for now. Add the elmah section and errorLog element to your web.config.
    <errorLog type="Elmah.MemoryErrorLog, Elmah" size="250" />
Remote access

By default you can only see the logging on the local machine. To enable remote access, you have to add the security element to the elmah section.
    <security allowRemoteAccess="1" />
    <errorLog type="Elmah.MemoryErrorLog, Elmah" size="250" />

Now that you're allowing remote access, you want to secure the logging page. I used ASP.NET authorization to achieve this.

Add the following element to your web.config, and make sure you change the authorization configuration to the relevant values.
<location path="elmah.axd">
        <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
        <allow roles="SuperUser"/>
        <deny users="*" />
        <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />

Push these changes, and you should be done. Navigate to elmah.axd in the root of your web application and you should see something like this.

It should be easier now to start playing with more advanced ELMAH options.


  1. Don't forget that you don't even have to push the NuGet package to AppHarbor, you can have it installed automatically when we build the project: