Click here to Skip to main content
15,072,051 members
Articles / Web Development / ASP.NET
Technical Blog
Posted 27 Dec 2012


24 bookmarked


Rate me:
Please Sign up or sign in to vote.
4.89/5 (5 votes)
27 Dec 2012CPOL3 min read
This .NET Tutorial explains how to set up and configure ELMAH (Error Logging Modules and Handlers) in an .NET 4.5 MVC 4 application


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 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.
  1. Ok, so firstly, install the Elmah package from the Package Manager console in Visual Studio:
    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).
  2. Now open your web.config and find the <elmah> section.  You'll need to modify it to use SQL logging.  Make that section look something like this:
      <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="YourConnectionStringName" applicationName="YourWebsiteName" />
      <security allowRemoteAccess="true" />
    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.
  3. 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">
            <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
            <allow roles="Admin"/>
            <deny users="*"/>
            <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
    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
  4. 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" />

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
        End If
    End Sub
End Class

Public Class ElmahMVCErrorAttribute
    Implements IExceptionFilter

    Public Sub OnException(filterContext As ExceptionContext) Implements IExceptionFilter.OnException
        If filterContext.Exception IsNot Nothing Then
        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:
And that's it, you're all configured and ready to go, if you go to, you should see a screen similar to the following:
Elmah GUI Example
You can drill down further, by clicking details, which will show you the stack trace if one is available.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Karl Stoney
Architect Hewlett Packard Enterprise Security Services
United Kingdom United Kingdom
Technical Architect for Hewlett-Packard Enterprise Security Service.

Please take the time to visit my site

Comments and Discussions

SuggestionSQL Script For ELMAH Pin
Amit R Jaiswal10-Oct-17 21:05
professionalAmit R Jaiswal10-Oct-17 21:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.