Best way to log errors in WCF

What's the best way to catch and log errors when developing a WCF service layer, and why?

I can think of three ways,

1) Manual try/catches around each method.

2) Leave the responsibility to the WCF engine.

3) Use a third party library such as Enterprise Library Policy Injection/Logging.


I would implement custom IErrorHandler and use log4net

[AttributeUsage (AttributeTargets.Interface)]
public class ErrorPolicyBehaviorAttribute : Attribute, IContractBehavior, IErrorHandler
    private ILog m_logger;

    #region IErrorHandler

    public void ProvideFault (Exception error, MessageVersion version, ref Message fault)

    public bool HandleError (Exception error)
        m_logger.Error (error.Message, error);
        return true;


    #region IContractBehavior

    public void ApplyDispatchBehavior (ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
        ...init logger
        ......Add this class to a list of dispatchRuntime.ChannelDispatcher.ErrorHandlers...


This class also implements IContractBehavior, so you can use it as Attribute on your service contracts.

public interface IYourServiceContract
{ }

log4net is quite flexible, so you can log what you need and when you need.

WCF can be configured to output traces for process milestones across all components of the applications, such as operation calls, code exceptions, warnings and other significant processing events.

The following is an app.config example to enable tracing.

      <source name="System.ServiceModel" switchValue="Warning" propagateActivity="true" >
          <add name="xml"/>

      <source name="myUserTraceSource" switchValue="Warning, ActivityTracing">
          <add name="xml"/>

      <add name="xml" 
           initializeData="TraceLog.svclog" />


You can read more about WCF Tracing from MSDN: Configuring Tracing.

Microsoft provides a Service Trace Viewer Tool to read .svclog files.

Apart from tracing, you may also want to consider using log4net for in-application logging.

If you are asking for logging framework ELMAH is also a good option to consider. If you dont like to litter your code with try/catch around each method, you can try using AOP frameworks which would give you the ability to handle exceptions by marking the method with Attributes

It might be worth your while to check out log4net. There is a good tutorial here on CodeProject.

I would go with number 1. Mainly because of reduced overhead over number 3 and number 2 should just be a no-no.

Granted you still want to log to something say like a file or event manager. But I personnally would use log4net for it since it's a bit lighter than all the entlib stuff.

Need Your Help

ActiveAdmin nested form on #show page

ruby-on-rails ruby-on-rails-3 activeadmin

Is it possible to add nested form to #show page?

Should repositories implement IQueryable<T>?

.net linq design-patterns

I'm considering one of two IRepository interfaces, one that is a descendant of IQueryable and one that contains IQueryable.