title: WCF, IErrorHandler and log4Net
body: <p>I am trying to implement an error handling behavior on a wcf service that will use log4net to log an exception</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Class)]
  public class AErrorHandlerBehaviorAttribute : Attribute, IServiceBehavior, IErrorHandler{
  
    private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  
    protected Type ServiceType { get; set; }
    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
     //Dont do anything
    }
  
    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection &amp;lt;ServiceEndpoint&amp;gt; endpoints, BindingParameterCollection bindingParameters)
    {
     //dont do anything
    }
  
    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
     ServiceType = serviceDescription.ServiceType;
     foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
     {
      dispatcher.ErrorHandlers.Add(this);
     }
    }
  
    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
     fault = null; //Suppress any faults in contract
    }
  
    public bool HandleError(Exception error)
    {   
     log.Error("Page Load failed : " + error.Message); 
     return false;
    }
  }
  </code></pre>
  
  <p>I then implement a service that uses the behavior. This works fine if I declare the ILog variable within the service</p>
  
  <pre><code>[AErrorHandlerBehavior]
  public class AService : IAService
  {
  
       private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  
          ....//various methods
  }
  </code></pre>
  
  <p>However, when the ILog variable is not declared the logging stops working.</p>
  
  <pre><code>[AErrorHandlerBehavior]
  public class AService : IAService
  {
  
       //private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  
          ....//various methods
  }
  </code></pre>
  
  <p>Ideally I don't want to have to declare the ILog variable in every service I produce, especially when it is already declared in the behavior.</p>
  
  <p>Could someone please explain a) why this has to be declared in both the behavior and in the service. and b) any way of avoiding the double declaration or c) a better way of logging in wcf.</p>