log4net - LogicalThreadContext - and unit test cases log4net - LogicalThreadContext - and unit test cases multithreading multithreading

log4net - LogicalThreadContext - and unit test cases


I ended up doing this to get it working:

put this in the TestCleanup() method:

CallContext.FreeNamedDataSlot("log4net.Util.LogicalThreadContextProperties");


So, I can't thank you enough for figuring this out to call FreeNamedDataSlot. This turned me on to my answer that worked for me. Instead of passing in the full namespace of the class, I just had to use the class name:

This was used somewhere deep in my Data Access Layer:

MySession session  = (MySession)System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("MySession");[TestCleanup]public void Cleanup(){    CallContext.FreeNamedDataSlot("MySession");}

This worked perfect for me! Hopefully this helps someone else when using Visual Studio's Test environment.


I know this is a bit old but for the following worked for me.

Although log4net was referenced by my project and my unit tests, it was not configured in the unit test. Updating my ThreadContext helper to the following allowed my tests to succeed. If log4net is configured in your unit tests and you are still getting this issue you could key off a compilation symbol instead.

var is_configured = log4net.LogManager.GetRepository().Configured;var props = is_configured            ? (ContextPropertiesBase)LogicalThreadContext.Properties            : (ContextPropertiesBase)ThreadContext.Properties;props[key] = value;