Log4net LogicalThreadContext not working as expected

前端 未结 2 1805
说谎
说谎 2020-12-17 19:44

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

相关标签:
2条回答
  • 2020-12-17 19:52

    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.

    0 讨论(0)
  • 2020-12-17 19:53

    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.

    0 讨论(0)
提交回复
热议问题