How to Create an Error Handler to Log Details of Faults for Auditing Purposes

- J.D. Meier, Carlos Farre, Jason Taylor, Prashant Bansode, Steve Gregersen, Madhu Sundararajan, Rob Boucher

You can create an error handler to log the fault details by implementing the IErrorHandler interface methods in your service. By using this you log and suppress the exceptions or you can log and throw them as FaultException.
public interface IErrorHandler 
{ 
        bool HandleError(Exception error, MessageFault fault); 
        void ProvideFault(Exception error, ref MessageFault fault, ref string faultAction); 
} 

To suppress the fault message, implement the HandleError method and return false. In this method you can add your code for logging capabilities.

To raise FaultException instead of suppressing, implement the ProvideFault method to provide the MessageFault value. The following code shows a sample implementation of the ProvideFault method
//code snippet
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
  FaultException newEx = new FaultException();
  MessageFault msgFault = newEx.CreateMessageFault();
  fault = Message.CreateMessage(version, msgFault, newEx.Action);
}

Additional Resources

Last edited Jun 13, 2008 at 7:42 PM by prashantbansode, version 1

Comments

FabioCozzolino Feb 16, 2009 at 3:12 PM 
I think there is an error in the interface. The right interface is:

public interface IErrorHandler
{
bool HandleError(Exception error);
void ProvideFault(Exception error, MessageVersion version, ref Message fault);
}

bye