5 Feb

Integrating ELMAH for a WCF Service

Category:UncategorizedTag: :

Peter Cosemans, who is one of my colleagues, found a nice way to integrate ELMAH for a WCF service. ELMAH is an error logging facility for logging unhandled exceptions particularly focused on ASP.NET web applications. There are plenty of sources out there, like this blog post by Scott Hanselman, that describe how to get ELMAH up and running for an ASP.NET web application.

In order to get it working for WCF, you need to provide a custom error handler by implementing the IErrorHandler interface:

public class ElmahErrorHandler : IErrorHandler
    public void ProvideFault(Exception error, MessageVersion version, 
                             ref Message fault)
        var dummyRequest = 
            new SimpleWorkerRequest("dummy", "", new StringWriter());
        var context = new HttpContext(dummyRequest);
        var elmahLogger = Elmah.ErrorLog.GetDefault(context);
        elmahLogger.Log(new Elmah.Error(error));    

    public Boolean HandleError(Exception error)
        return true;


This error handler needs to be added to the stack of error handlers. You can do this in a couple of ways, for example by providing a custom attribute that implements IServiceBehavior and then applying this attribute to your service class.

Next you need to add some configuration to your web.config and your all good to go:

        <add verb="POST,GET,HEAD" 
             type="Elmah.ErrorLogPageFactory, Elmah" />

    <errorLog type="Elmah.XmlFileErrorLog, Elmah" 


What’s nice about this approach is that you don’t need to run the WCF service in ASP.NET compatibility mode, which is a major bonus.

Hope this helps

2 thoughts on “Integrating ELMAH for a WCF Service

  1. Thanks for the post.
    I followed it, made a custom service behavior attribute, but ElmahErrorHandler was never passed through. Can you go into more details or provide a demo pleeeeeeaaase?


Comments are closed.