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.

Installation

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 1.2.0.1'.
Successfully added 'elmah 1.2.0.1' 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.
<elmah>    
    <errorLog type="Elmah.MemoryErrorLog, Elmah" size="250" />
</elmah>
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.
<elmah>
    <security allowRemoteAccess="1" />
    <errorLog type="Elmah.MemoryErrorLog, Elmah" size="250" />
</elmah>
Security

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">
    <system.web>
      <httpHandlers>
        <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
      </httpHandlers>
      <authorization>
        <allow roles="SuperUser"/>
        <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>
Finished

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.

2 comments:

  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: http://blog.appharbor.com/2012/02/06/use-nuget-package-restore-to-avoid-pushing-packages-to-appharbor

    ReplyDelete