Take the 2-minute tour ×

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:

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
      //...Log4net logging

If I let the program continue the threads are still stuck on that line.

I can't see where this can lock. The debug class, string class & datetime class seem to be thread safe.

The error goes away when I remove the #if DEBUG System... #endif code but I'm curious why this behavior happens.

Thread one:

public void CleanCache()
    Logging.WriteClientLog(LogLevel.Debug, "Start clean cache.");//Stuck

Thread two:

private void AliveThread()
    Logging.WriteClientLog(LogLevel.Debug, "Check connection");//Stuck
share|improve this question
So if you just remove the if debug and endif messages, but leave the write line in the method, it works fine? –  Tejs May 3 '10 at 15:55
Do you have custom trace listeners configured? –  Chris Taylor May 3 '10 at 16:01
i've run into a similar issue...which ended in me switching to using MS's enterprise libary logging. made for far less headaches. –  Jason M May 3 '10 at 16:56
No custom trace listeners, I only see the DefaultraceListener in the Debug list. –  Carra May 4 '10 at 9:10

1 Answer 1

up vote 3 down vote accepted

Debug.WriteLine writes logging messages to the attached Trace Listeners attached to the Listeners collection.

One of your trace listeners must have a lock internally which is causing a deadlock. Check your listener code, as it's most likely the culprit.

share|improve this answer

Your Answer


