log4net的LogicalThreadContext未正常工作 [英] Log4net LogicalThreadContext not working as expected

查看:169
本文介绍了log4net的LogicalThreadContext未正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Log4nets LogicalThreadContext提供上下文我的每一个日志条目。我的应用程序使用异步/等待相当严重,但是从阅读各项条款的LogicalThreadContext应适当异步code工作,从.NET 4.5起。我使用.NET 4.5.1和2.0.3 log4net的

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 articles the LogicalThreadContext should work properly with asynchronous code, from .NET 4.5 onwards. I'm using .NET 4.5.1 and log4net 2.0.3

我碰到一个很大的文章来到斯蒂芬·克利有关记录和.NET CallContext中,并作为一个结果,我决定把他的code和适应其使用log4net的,试图看看是否有在我的code一些错误可能已导致此问题。

I came across a great article by Stephen Cleary about logging and the .NET CallContext, and as a result I decided to take his code and adapt it to use log4net, in an attempt to see if there was something wrong in my code that may have been causing the issue.

首先,我跑斯蒂芬斯code完全一样的,并得到像这样的预期输出

Firstly, I ran Stephens code exactly as is and got the expected output like so

Main 1: <SomeWork>
Main 1 A: <MoreWork>
Main 2: <SomeWork>
Main 2 A: <MoreWork>
Main 1 A: </MoreWork>
Main 1 B: <MoreWork>
Main 2 A: </MoreWork>
Main 2 B: <MoreWork>
Main 2 B: </MoreWork>
Main 2: </SomeWork>
Main 1 B: </MoreWork>
Main 1: </SomeWork>

接下来,我修改了code使用log4net的,而不是斯蒂芬斯定制 MyStack

internal class Program
{
    private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
    private const string StackName = "test";

    private static void Main(string[] args)
    {
        XmlConfigurator.Configure();

        using (LogicalThreadContext.Stacks[StackName].Push("Main"))
        {
            Task.WhenAll(SomeWork("1"), SomeWork("2")).Wait();
        }

        Console.ReadKey();
    }

    private static async Task SomeWork(string stackName)
    {
        using (LogicalThreadContext.Stacks[StackName].Push(stackName))
        {
            Log.Info("<SomeWork>");
            await MoreWork("A");
            await MoreWork("B");
            Log.Info("</SomeWork>");
        }
    }

    private static async Task MoreWork(string stackName)
    {
        using (LogicalThreadContext.Stacks[StackName].Push(stackName))
        {
            Log.Info("<MoreWork>");
            await Task.Delay(10);
            Log.Info("</MoreWork>");
        }
    }
}

我希望像以前一样得到类似的输出,而是这次我得到了以下

I expected to get a similar output as before, but instead this time I got the following

Main 1: <SomeWork>
Main 1 A: <MoreWork>
Main 1 A 2: <SomeWork>
Main 1 A 2 A: <MoreWork>
Main 1 A 2 A: </MoreWork>
Main 1 B: <MoreWork>
Main 1 B: </MoreWork>
Main 1 B B: <MoreWork>
Main 1 B B: </MoreWork>
Main 1 B: </MoreWork>
Main 1: </SomeWork>
Main 1: </SomeWork>

注意单独的逻辑线程上下文开始重叠。这表明对我的log4net的不正确使用CallContext中但一切我可以找到它看起来像他们已经固定解决此的任何问题。

Notice the separate logical thread contexts start to overlap. This indicates to me the log4net isn't using the CallContext correctly but from everything I can find it looks like they've already fixed any issues around this.

有没有其他人遇到这样或知道为什么这可能发生?

Has anyone else encountered this or know why this might be happening?

推荐答案

不幸的是,它看起来像log4net的的还是的有异步不起作用逻辑栈。

Unfortunately, it looks like log4net still does not work with async logical stacks.

log4net的的NuGet包2.0.3是log4net的1.2.13(当前版本截至今日)。有报道中的错误( log4net的-317 ),因为当时 LogicalThreadContext 使用 CallContext中而不是 LogicalCallContext

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.

的<一个href=\"http://svn.apache.org/viewvc/logging/log4net/trunk/src/log4net/LogicalThreadContext.cs?revision=1539082&view=markup\">current LogicalThreadContext 的版本并使用 LogicalThreadContext ,而<一个href=\"http://svn.apache.org/viewvc/logging/log4net/trunk/src/log4net/Util/ThreadContextStacks.cs?revision=1539082&view=markup\">current ThreadContextStacks 的版本确实的的正确使用一个不变的堆栈。

The current version of LogicalThreadContext does use LogicalThreadContext, but the current version of ThreadContextStacks does not correctly use an immutable stack.

有趣的是,<一href=\"http://logging.markmail.org/thread/q3bogptirf32g77r#query%3a+page:1+mid%3aouwfvitbd5bjjgz4+state%3aresults\">Andrew阿诺特在原来的bug报告,他们不得不使用一个不变的堆栈正确地指出。 2011年。

Interestingly, Andrew Arnott correctly pointed out in the original bug report that they had to use an immutable stack. In 2011.

随便写了一个最小的摄制,并报告为log4net的错误。关键场景是使用使用合​​并多个任务中的堆栈 Task.WhenAll

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.

这篇关于log4net的LogicalThreadContext未正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆