Implementing WCF IErrorHandler for logging only
Asked Answered
M

2

7

Probably trivial question.. I want to implement my own error handler to log errors and monitor what's going on. At this point I don't want to provide my own faults to the clients. I want it to be transparent - just like default WCF behavior. How should I implement ProvideFault to achieve this?

namespace IDATT.Web.Services
{
    using System;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Dispatcher;

    public class MyServiceErrorHandler : IErrorHandler 
    {
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            // ????
        }

        public bool HandleError(Exception error)
        {
            return true;
        }
    }
}
Maurine answered 13/6, 2012 at 20:17 Comment(0)
P
3

You can leave it empty. I do this to log errors to Elmah with no issues.

EDIT

I'm completely wrong on this. After looking at my implementation I do the following. As you can see, HandleError is the method that is basically empty and the logging takes place in ProvideFault.

public class ElmahErrorHandler : IErrorHandler
{
    public bool HandleError(Exception error)
    {
        return false;
    }


    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
        if (error == null)
        {
            return;
        }

        ErrorSignal.FromCurrentContext().Raise(error);
    }
}

Returning true in HandleError will make WCF think that no error occured. That means if you are just logging the error, this should always return false. If the error is not handled, ProvideFault is called and this is where you want to do the logging. You do not need to provide a fault Message.

Pumpkin answered 13/6, 2012 at 20:24 Comment(6)
What exactly ErrorSignl.FromCurrentContext().Raise(error) does? And why you return "false" from HandleError?Maurine
That is a specific call to Elmah to log the error. As far as why I return false, please see the updated answer.Pumpkin
Ok, if I return false then I can just leave ProvideFault empty? Is that correct and it won't be even called?Maurine
The typical pattern is that HandleError should either correct the error and return true or return false. Then, if the error is not handled(HandleError returned false), log the error in ProvideFault.Pumpkin
Note: MSDN says to log the exception in HandleError, but every example I've seen does it in ProvideFault. I assume this is because there is no guarantee which thread HandleError will be called on.Pumpkin
Note that ProvideFault() is called first, then eventually HandleError() is called, as the MSDN: msdn.microsoft.com/en-us/library/…Curator
M
2

From the documentation (http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler.aspx):

Implement the HandleError method to ensure error-related behaviors, including error logging, assuring a fail fast, shutting down the application, and so on.

Also the link above notes that only HandleError is called after the response has been sent back to the client. So be nice to your client (don't make them wait while you log), leave ProvideFault blank and perform logging operations in HandleError.

Messily answered 7/2, 2014 at 1:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.