Best way to log errors in WCF

27,131

Solution 1

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)
        {
        return;
        }

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

    #endregion

    #region IContractBehavior

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

    #endregion
    }

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

[ErrorPolicyBehavior]
public interface IYourServiceContract
{ }

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

Solution 2

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.

<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Warning" propagateActivity="true" >
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>

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

    <sharedListeners>
      <add name="xml" 
           type="System.Diagnostics.XmlWriterTraceListener" 
           initializeData="TraceLog.svclog" />
    </sharedListeners>

  </system.diagnostics>
</configuration>

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.

Solution 3

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

Solution 4

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

Solution 5

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.

Share:
27,131
Kye
Author by

Kye

Updated on October 20, 2020

Comments

  • Kye
    Kye over 3 years

    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.