I\'ve been trying to use Log4nets LogicalThreadContext to provide context to each of my log entries. My application uses async/await quite heavily, but from reading various
Unfortunately, it looks like log4net still does not work with async
logical stacks.
The log4net NuGet package 2.0.3 is log4net 1.2.13 (the current version as of today). There was a bug reported (LOG4NET-317) because at that time LogicalThreadContext
used CallContext
instead of LogicalCallContext
.
The current version of LogicalThreadContext does use LogicalThreadContext
, but the current version of ThreadContextStacks does not correctly use an immutable stack.
Interestingly, Andrew Arnott correctly pointed out in the original bug report that they had to use an immutable stack. In 2011.
Feel free to write up a minimal repro and report it as a log4net bug. The key scenario is using the stack within multiple tasks that are combined using Task.WhenAll
.
I just ran into this on one of our projects. This should fix it: https://github.com/apache/log4net/pull/12.
The code basically obeys to the shallow-copy-on-write
and immutable
rules described by @StephenCleary and the accompanied test case withstands the Task.WhenAll
scenario.