title: How to log correct context with Threadpool threads using log4net?
body: <p>I am trying to find a way to log  useful context from a bunch of threads. The problem is that a lot of code is dealt with on Events that are arriving via threadpool threads (as far as I can tell) so their names are not in relation to any context. The problem can be demonstrated with the following code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>class Program
  {
      private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
      static void Main(string[] args)
      {
          new Thread(TestThis).Start("ThreadA");
          new Thread(TestThis).Start("ThreadB");
          Console.ReadLine();
      }
  
      private static void TestThis(object name)
      {
          var nameStr = (string)name;
          Thread.CurrentThread.Name = nameStr;
          log4net.ThreadContext.Properties["ThreadContext"] = nameStr;
          log4net.LogicalThreadContext.Properties["LogicalThreadContext"] = nameStr;
          log.Debug("From Thread itself");
          ThreadPool.QueueUserWorkItem(x =&gt; log.Debug("From threadpool Thread: " + nameStr));
      }
  }
  </code></pre>
  
  <p>The Conversion pattern is: </p>
  
  <pre><code>%date [%thread] %-5level %logger [%property] - %message%newline
  </code></pre>
  
  <p>The output is like so:</p>
  
  <pre><code>2010-05-21 15:08:02,357 [ThreadA] DEBUG LogicalContextTest.Program [{LogicalThreadContext=ThreadA, log4net:HostName=xxx, ThreadContext=ThreadA}] - From Thread itself
  2010-05-21 15:08:02,357 [ThreadB] DEBUG LogicalContextTest.Program [{LogicalThreadContext=ThreadB, log4net:HostName=xxx, ThreadContext=ThreadB}] - From Thread itself
  2010-05-21 15:08:02,404 [7] DEBUG LogicalContextTest.Program [{log4net:HostName=xxx}] - From threadpool Thread: ThreadA
  2010-05-21 15:08:02,420 [16] DEBUG LogicalContextTest.Program [{log4net:HostName=xxx}] - From threadpool Thread: ThreadB
  </code></pre>
  
  <p>As you can see the last two rows have no Names of useful information to distinguish the 2 threads, other than manually adding the name to the message (which I want to avoid). How can I get the Name/Context into the log for the threadpool threads without adding it to the message at every call or needing to set the property again in every callback.</p>