wcf Handling exceptions Using a custom error logging framework


Example

It is sometimes useful to integrate a custom error logging framework to ensure all exceptions are logged.

[ServiceContract]
[ErrorHandler]
public interface IMyService
{
       
}

[AttributeUsage(AttributeTargets.Interface)]
public class CustomErrorHandler : Attribute, IContractBehavior, IErrorHandler
{
    public bool HandleError(Exception error)
    {
        return false;
    }

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

        //my custom logging framework
    }

    public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    {
        dispatchRuntime.ChannelDispatcher.ErrorHandlers.Add(this);
    }

    public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint,
        ClientRuntime clientRuntime)
    {

    }

    public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint,
        BindingParameterCollection bindingParameters)
    {

    }

    public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    {

    }
}