title: Logging frameworks and multithread compatibility
body: <p>Note that I have seen these questions here on SO (which discuss the thread safety of log4net) and I suspect that they answer my question, but I am going to ask anyway:</p>
  
  <p><a href="https://stackoverflow.com/questions/1519211/multithread-safe-logging">Multithread safe logging</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1294668/log4net-fileappender-not-thread-safe">Log4Net FileAppender not thread safe?</a></p>
  
  <p>Recently I have written a WCF service for logging.  The idea is very similar to <a href="http://clog.codeplex.com/" rel="nofollow noreferrer">Clog</a> (or <a href="http://calcium.codeplex.com/" rel="nofollow noreferrer">look for Clog under Calcium</a>).  Basically, I have implemented a logging API for use in a Silverlight client (a Silverlight class library).  The logging API is, more or less, a clone of the <a href="http://netcommon.sourceforge.net/" rel="nofollow noreferrer">Common.Logging for .NET API</a> which we are using elsewhere in our application.  The implementation of the API forwards all logging messages to a WCF logging service that, itself, is implemented in terms of Common.Logging.</p>
  
  <p>While looking at Clog I found the following code in the <code>Log4NetStrategy</code> class that struck me as a little bit odd:</p>
  
  <pre><code>/// &lt;summary&gt; 
  /// All Write log calls are done asynchronously because the DanielVaughan.Logging.Log 
  /// uses the AppPool to dispatch calls to this method. Therefore we need to ensure 
  /// that the call to Log is threadsafe. That is, if an appender such as FileAppender is used, 
  /// then we need to ensure it is called from no more than one thread at a time. 
  /// &lt;/summary&gt;
  /// &lt;param name="logger"&gt;The logger.&lt;/param&gt;
  /// &lt;param name="loggingEvent"&gt;The logging event.&lt;/param&gt;
  [MethodImpl(MethodImplOptions.Synchronized)]
  static void WriteThreadSafe(ILogger logger, LoggingEvent loggingEvent)
  {
    logger.Log(loggingEvent);
  }
  </code></pre>
  
  <p><code>Log4NetStrategy</code> (and the strategies for NLog and EnterpriseLibrary as well) implements an interface that has a method like Write(ILogEntry logEntry).  ILogEntry is essentially the DTO that the Clog logging service received from the client.  The information from the logEntry is extracted and used to create a log4net LoggingEvent.  The appropriate log4net logger is also retrieved based on the logger name in the ILogEntry DTO.  After the log4net LoggingEvent is created, it and the logger are sent to the WriteThreadSafe method above and logged via log4net.  The NLog and EnterpriesLibrary implementations are similar.</p>
  
  <p>In pseudocode, the "Write" method on the Clog logging service looks something like this:</p>
  
  <pre><code>// Write on the logging service
  // Send the input log entry to each configured strategy.
  public void Write(ILogEntry logEntry)
  {
    foreach (ILoggingStrategy ls in loggingStrategies)
    {
      ls.Write(logEntry);
    }
  }
  
  // Write on the Log4NetStrategy
  // Convert the logEntry to log4net form and log with log4net
  public void Write(ILogEntry logEntry)
  {
    log4net.LoggingEvent le = ConvertToLoggingEvent(logEntry);
    ILog logger = log4net.GetLogger(logEntry.Logger);
    WriteThreadSafe(logger, le);
  }
  </code></pre>
  
  <p>So here is my question...  Generally log4net (and NLog and, I assume, EnterpriseLibrary) are considered to be multithread compatible.  That is, the user of the public API can simply call log.Info, log.Log, etc and not be concerned about running in a multithreaded environment.  The logging framework should take care of ensuring that the logging calls (and any processing within the logging calls) are threadsafe.</p>
  
  <p>If the logging frameworks are multithread compatible, then is the use of the </p>
  
  <pre><code>[MethodImpl(MethodImplOptions.Synchronized]
  </code></pre>
  
  <p>attribute really needed?  It seems like this would (or could) just cause a bottleneck by forcing all logging messages to be handled synchronously, even though the underlying logging framework should be able to handle logging in a multhithreaded environment.</p>
  
  <p>In the case of my logging service (which could be multithreaded), it doesn't seem like it would be necessary to synchronize the calls like this.  It seems like I should be able to take my logging input from the service call, construct the appropriate logging structure (based on the underlying logging framework), and just log it.  If my logging service is multithreaded, it should "just work" because the underlying logging framework should support it (multithreading).</p>
  
  <p>Does this make sense?  Is the explicit synchronization of logging calls (at least for log4net and NLog) really necessary?</p>