I use MappedDiagnosticsLogicalContext, because I need data to flow in async calls. This is piece of my NLog.config:
<column name="UserId" layout="${mdlc:item=UserId}" />
But in some cases I need to disable flowing, and I don't know how to do it. For example, I have code that creates several threads (see comments):
void Run()
{
logger.Info("Starting threads..."); // MappedDiagnosticsLogicalContext.LogicalThreadDictionary is called and ConcurrentDictionary is created
(new Thread(Func1)).Start(); // both threads will refer to the same ConcurrentDictionary object and share common values, because MappedDiagnosticsLogicalContext stores data in logical call context
(new Thread(Func2)).Start();
}
void Func1()
{
MappedDiagnosticsLogicalContext.Set("UserId", "123");
logger.Info("Some message for Func1");
}
void Func2()
{
Thread.Sleep(5000);
logger.Info("Some message for Func2"); // UserId will be added to the log message here (because it was added in other thread), but I don't need it
}
I have found dirty workaround: at the beginning of the Func2(), I call
CallContext.FreeNamedDataSlot("NLog.AsyncableMappedDiagnosticsContext")
Thus, when logging occurs in Func2(), getter of property MappedDiagnosticsLogicalContext.LogicalThreadDictionary will create new ConcurrentDictionary and threads will have separate instances of dictionaries. It would be good to have such a functionality in MappedDiagnosticsLogicalContext class. Or are there some other ways to fix problem?