My Adventures in Coding

July 5, 2012

C# – Logging Exceptions with Elmah in an MVC 4 Web API Application

Filed under: .NET,c#,MVC — Brian @ 11:08 pm
Tags: , ,

Recently, after having been away from the .NET world for several years, I have started on a new project where we are building REST APIs using MVC 4. One of the first things we wanted to find out how to do was have all exceptions written to a database, as well as have a web interface from which to view exception details. Fortunately there is a very nice open source project available called Elmah.

When I created my first MVC 4 Web API application using Elmah, I was very rusty with .NET so getting Elmah setup and working took me a little longer than expected. Part of the reason was because the setup required piecing together information from multiple sources, since things have changed a bit in MVC 4. So here is a very simple quick start tutortial to help you get up and running with Elmah. If you would like to learn more about Elmah, the following article gives a good overview: Logging Error Details with ELMAH (C#).

NOTE: If you are using NuGet, you can use the Elmah.MVC NuGet Package to install Elmah, which works great. However, if you are not using NuGet, the following instructions will walk you through the setup.

1. Create a new MVC 4 Web API Project

The following article is useful, if you have never created an MVC 4 Web API application before:
ASP.NET WebAPI: Getting Started with MVC4 and WebAPI

2. Download the latest version of Elmah

You can obtain the latest version of Elmah here. At the time of this post, I am using version 1.2 Service Pack 2.

Unpack the zip file ELMAH-1.2-sp2-bin-x64.zip and add the following dll to your project as a reference:
…\ELMAH-1.2-sp2-bin-x64\bin\net-2.0\Release\Elmah.dll

3. Setup Elmah in your Web.config file

To the configSections part of your Web.config, add an Elmah sectionGroup:

<configSections>
<sectionGroup name="elmah">
      <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
      <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
      <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
      <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
    </sectionGroup>
…
</configSections>

To log to a Microsoft SQL Server database (In our case we are using SQL Server 2005), add a connection string for Elmah filling in your database server name and the name of your exceptions database:

<connectionStrings>
        <add name="elmah-sql" connectionString="Data Source=MY_DB_HOST;Initial Catalog=MY_EXCEPTIONS_DB_NAME;Integrated Security=SSPI;Connection Timeout=60;Min Pool Size=2;Max Pool Size=20;" providerName="System.Data.SqlClient" />
  </connectionStrings>

Inside the configuration tag add an Elmah section setting the “applicationName” to be some unique string to identify your application:

<configuration>
    <elmah>
        <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="elmah-sql" applicationName="SETMYAPPLICATIONNAME"></errorLog>
        <security allowRemoteAccess="1" />
    </elmah>
    ...
</configuration>

Inside the system.web tag add:

<system.web>
    <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
    </httpModules>
    <httpHandlers>
      <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>
    ...
</system.web>

In the system.webServer tag add:

<system.webServer>
    <handlers>
      <add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </handlers>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
    </modules>
    ...
</system.webServer>

4. Run the Elmah SQL Server database setup script on your Exceptions database

You need to apply the script that creates the Elmah table where exception information will be logged, as well as adds the stored procedure required by the web interface to read exception messages. The script is located in the zip file you downloaded, located at:
…\ELMAH-1.2-sp2-bin-x64\db\SQLServer.sql.

5. Create a custom ExceptionFilterAttribute class

Even though, up to this point we have completed all the required setup for Elmah, it will still not work in our MVC 4 Web API project. To log exceptions to Elmah you must create a custom ExceptionFilterAttribute class and use that to call Elmah when an exception occurs.

public class ElmahErrorAttribute : System.Web.Http.Filters.ExceptionFilterAttribute
{
    public override void OnException(
         System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
    {
        if (actionExecutedContext.Exception != null)
            Elmah.ErrorSignal.FromCurrentContext().Raise(actionExecutedContext.Exception);
        base.OnException(actionExecutedContext);
    }
}

6. Register the custom ExceptionFilterAttribute class in your global.ascx.cs file

Finally, you must register your custom ExceptionFilterAttribute class on startup.

protected void Application_Start()
{
    GlobalConfiguration.Configuration.Filters.Add(
        new ElmahErrorAttribute()
    );
    ….
}

That’s it! Now you should be able to run your web app and go to the page:

http://localhost:SOMEPORT/elmah.axd

Advertisements

12 Comments »

  1. Worked like a charm! I had to migrate my application to MVC4 from MVC2 beforehand however 😛 Otherwise an extremely easy to follow tutorial. Thanks!

    Comment by Douglas — December 4, 2012 @ 4:47 pm | Reply

    • Really Helpful steps. Using this step I was able to implement the logging feature with 1-2 hrs..Thank you so much..

      Comment by Rakesh — March 12, 2013 @ 12:26 pm | Reply

  2. Apparently I didn’t need steps 5 & 6. I am using VS2012 and installed Elmah from Nuget. Some of the web.config settings were automatically added but I had to add others manually.

    Comment by MarkusR — March 27, 2013 @ 1:38 pm | Reply

    • Thanks for the feedback! Yeah back when I wrote this article steps 5 & 6 were gotchas, but fortunately now those steps are no longer needed in the newest versions of the NuGet package. I recently added Elmah to a new MVC 4 Web API project and also noticed I no longer needed to do those steps, which was great. For the Web.config setup, I hope newer versions of the Elmah NuGet package will do the complete Web.config setup.

      Comment by Brian — March 27, 2013 @ 1:53 pm | Reply

      • maybe edit your post to include this info, its the #1 post when googling elmah mvc 4

        Comment by thomas — October 18, 2013 @ 3:30 am

  3. A very useful post. Thank you.

    Comment by NeilT — April 24, 2014 @ 10:16 am | Reply

  4. thank you!! This is what I need to get Elmah up and running.

    Comment by measow — May 7, 2014 @ 10:22 am | Reply

  5. Can we still configure the elmah.axd url to get and show the error logs from the db? it seems once you set it up to log in sql server then logs won’t show anymore on the browser

    Comment by Allan — July 24, 2014 @ 11:40 pm | Reply

    • I take that back. it does show the errors from the db

      Comment by Allan — July 24, 2014 @ 11:47 pm | Reply

      • Great! Yeah you can log to the database and the elmah.axd page will show all the errors just fine. Really handy to have the option to query the database when looking back at older errors or when you want to find out how frequently something has happened. I have used Elmah on a number of apps now and really find it handy.

        Comment by Brian — July 24, 2014 @ 11:53 pm

  6. Hi,

    nice tutorial! In MVC 4 some settings were added automatically after install it. Can u tell me how to set a uniqe error page with simple text that is explained when error happen. The error massege should be written in the DB. Also, how to disable that the error log is accessible over the url http://www.app.com/elmah?

    Thanks

    Comment by Talk Master — October 24, 2014 @ 4:41 am | Reply

  7. Thank You – I’ve set up ELMAH many times before but never from a Web API – the last step on ElmahErrorAttribute was news to me – and made all the difference!

    Comment by David Laub — July 10, 2015 @ 8:35 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: