title: Debug.WriteLine locks
body: <p>My program frequently stops with a deadlock. When I do a break-all and look at the threads I see that three threads are stuck in our logging function:</p>
  
  <pre><code>public class Logging
  {
      public static void WriteClientLog(LogLevel logLevel, string message)
      {
        #if DEBUG
        System.Diagnostics.Debug.WriteLine(String.Format("{0} {1}", DateTime.Now.ToString("HH:mm:ss"), message)); //LOCK
        #endif
        //...Log4net logging
      }
  }
  </code></pre>
  
  <p>If I let the program continue the threads are still stuck on that line.</p>
  
  <p>I can't see where this can lock. The debug class, string class &amp; datetime class seem to be thread safe.</p>
  
  <p>The error goes away when I remove the <code>#if DEBUG System... #endif</code> code but I'm curious why this behavior happens.</p>
  
  <p>Thread one:</p>
  
  <pre><code>public void CleanCache()
  {
      Logging.WriteClientLog(LogLevel.Debug, "Start clean cache.");//Stuck
  }
  </code></pre>
  
  <p>Thread two:</p>
  
  <pre><code>private void AliveThread()
  {
      Logging.WriteClientLog(LogLevel.Debug, "Check connection");//Stuck
  }
  </code></pre>