title: What is the difference between log4net.ThreadContext and log4net.LogicalThreadContext?
body: <p>UPDATED on 11/18/2014 - While browsing the log4net source repository, I found that the implementation of LogicalThreadContext was modified in November 2011 to that it stores its properties using CallContext.LogicalSetData (and gets them using LogicalGetData).  This is important because that means that LogicalThreadContext should now work correctly.  Any data stored in LogicalThreadContext should be "flowed" to any child threads or tasks.  This compares to ThreadContext (and the old implementation of LogicalThreadContext) where data stored in the context would remain local to the current thread and NOT flowed to child threads/tasks.</p>
  
  <p>If you are interested, here is the change:</p>
  
  <p><a href="http://svn.apache.org/viewvc/logging/log4net/trunk/src/log4net/Util/LogicalThreadContextProperties.cs?r1=1165341&amp;r2=1207948&amp;diff_format=h" rel="nofollow noreferrer">http://svn.apache.org/viewvc/logging/log4net/trunk/src/log4net/Util/LogicalThreadContextProperties.cs?r1=1165341&amp;r2=1207948&amp;diff_format=h</a></p>
  
  <p>Hopefully someone happening upon this old question will find this information useful.</p>
  
  <p>log4net provides two different "thread context" objects:  <a href="http://logging.apache.org/log4net/release/sdk/log4net.ThreadContext.html" rel="nofollow noreferrer">ThreadContext</a> and <a href="http://logging.apache.org/log4net/release/sdk/log4net.LogicalThreadContext.html" rel="nofollow noreferrer">LogicalThreadContext</a>, each of which has a property bag, Properties.  ThreadContext has a <a href="http://logging.apache.org/log4net/release/sdk/log4net.Util.ThreadContextProperties.html" rel="nofollow noreferrer">ThreadContextProperties</a> bag while LogicalThreadContext has a <a href="http://logging.apache.org/log4net/release/sdk/log4net.Util.LogicalThreadContextProperties.html" rel="nofollow noreferrer">LogicalThreadContextProperties</a> bag.</p>
  
  <p>ThreadContext is perhaps more commonly known as "MDC".
  LogicalContext is perhaps more commonly known as "LDC".
  I will use the short name for the rest of this post.</p>
  
  <p>MDC.Properties is implemented using <a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.setdata.aspx" rel="nofollow noreferrer">System.Threading.Thread.SetData</a> while LDC.Properties is implemented using using <a href="http://msdn.microsoft.com/en-us/library/system.runtime.remoting.messaging.callcontext.setdata.aspx" rel="nofollow noreferrer">System.Runtime.Remoting.Messaging.CallContext.SetData</a>.</p>
  
  <p>For comparison, NLog only exposes "MDC" (now known as MappedDiagnosticContext) to store thread local properties.  NLog's implementation uses System.Threading.Thread.SetData, so its implementation is the same as log4net's.</p>
  
  <p>In both log4net and NLog, the "MDC" properties are stored in a dictionary which is itself stored in the thread local storage.  </p>
  
  <p>In a case like this, would storing the dictionary in a class member variable decorated with [ThreadStatic] have been equivalent?</p>
  
  <pre><code>[ThreadStatic]
  private static IDictionary&lt;string, string&gt; threadProperties;
  </code></pre>
  
  <p>What be the equivalent (or similar) declaration using .NET 4.0's new ThreadLocal class?</p>
  
  <p>Ultimately, what is the real, practical, difference between LDC and MDC?  Even after reading the MSDN topics linked above, it is not clear to me.  When would you really use one over the other?  It seems like the vast majority of the references/examples that I see for log4net and context is for GDC (global - which I understand), NDC (nested - which I also understand), and MDC.  Most of the references that I can find to LDC (or LogicalThreadContext) when googling are related to checkins into the log4net source code repositories, not real-world usage.  LDC almost never comes up in questions or examples.</p>
  
  <p>I did find <a href="http://www.l4ndash.com/Log4NetMailArchive%2Ftabid%2F70%2Fforumid%2F1%2Fpostid%2F15288%2Fview%2Ftopic%2FDefault.aspx" rel="nofollow noreferrer">this</a> link that offers some pretty good information about the difference from one of the log4net developers, Nicko Cadell, but it is still not clear to me.</p>
  
  <p>A larger question, not directly related to log4net is what is the practical difference between Thread.SetData and CallContext.SetData?</p>
  
  <p>According to the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.remoting.messaging.callcontext.aspx" rel="nofollow noreferrer">CallContext</a> MSDN article, CallContext data can be propagated to another AppDomain.  To be propagated, a data item stored in the CallContext must expose the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.remoting.messaging.ilogicalthreadaffinative.aspx" rel="nofollow noreferrer">ILogicalThreadAffinative</a> interface.  So, that seems to be one difference between Thread.SetData and CallContext.</p>
  
  <p>According the Nicko Cadell link, log4net does not implement ILogicalThreadAffinative, so the LDC properties will not be propagated.</p>
  
  <p>Maybe there is enough here that I should be able answer my own question, maybe not.  I am still working on understanding.</p>
  
  <p>If you use log4net, do you every use MDC, LDC, both?  If you use MDC, is it because most of the "real world" examples seem to use it?  If you use LDC, to you have a specific reason for using it?  If you use both, how do you choose when to use which?</p>
  
  <p>Note that I have seen some articles regarding MDC (and maybe LDC) maybe not working right in ASP.net applications due to thread switching.  I am not particularly interested in this problem as I am not working in ASP.net.</p>
  
  <p>Actually, I have found a couple of useful posts here on SO that might contribute to the discussion:</p>
  
  <p><a href="https://stackoverflow.com/questions/175129/what-are-best-practices-for-using-thread-local-storage-in-net">What are best practices for using thread local storage in .NET?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1007277/net-logical-thread-and-thread-local-storage">.Net: Logical thread and Thread Local Storage?</a></p>
  
  <p>Thanks in advance!</p>