ASP.NET Web API and ELMAH Integration

See how ASP.NET Web API Plays Nice With ELMAH. This blog post is a Quick introduction to ASP.NET Web API and System.Web.Http.Filters.IExceptionFilter
23 February 2012
3 minutes read

Related Posts

As you all probably heard, ASP.NET MVC 4 Beta is now available and has new features in it. One of them is that ASP.NET MVC has shipped with ASP.NET Web API (which was previously know as WCF Web API). Here is the quote from ASP.NET web site which explains what Web API Framework is all about shortly:

ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.

I am not going to give an into on ASP.NET Web API. There are great into tutorials and videos on ASP.NET web site for ASP.NET Web API. Instead, I will give you an example of a custom filter implementation.

A couple of months ago, I wrote a blog post about WCF Web API HttpErrorHandlers: WCF Web API Plays Nice With ELMAH - A Quick Introduction to WCF Web API HttpErrorHandler. It works nicely on WCF Web API but this version of the framework, things a little bit changed. Instead of ErrorHandlers, we now have Filters which is more generic.

In order to make ELMAH work with ASP.NET Web API, we need to create a new Attribute which implements IExceptionFilter interface. Since we have ExceptionFilterAttribute (which implements the IExceptionFilter interface) available at the framework, we will derived from that class instead. Here is the whole implementation:

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);
    }
}

Now we have our attribute, we need to tell the framework to use it. It is very straight forward as well. Here is how my Global.asax (Global.asax.cs) looks like:

public class WebApiApplication : System.Web.HttpApplication {

    protected void Application_Start() {

        Configure(
            System.Web.Http.GlobalConfiguration.Configuration
        );
    }

    private void Configure(HttpConfiguration httpConfiguration) {

        httpConfiguration.Filters.Add(
            new ElmahErrorAttribute()
        );

        httpConfiguration.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

This will make Elmah log the errors. Now, you can configure ELMAH to send you an e-mail when an error occurred or you can log the error inside an XML file, SQL Server Database, wherever you what.

One more thing to mention about is that when you hit an error, the response will carry the exception details at the body of the response if you run your application locally. You can configure this option as well with the following configuration:

httpConfiguration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Never;