【问题标题】:log4net became very slow with caller location information after Windows 10 Fall Creators Update (1709)在 Windows 10 Fall Creators Update (1709) 之后,log4net 的调用者位置信息变得非常慢
【发布时间】:2018-05-25 10:13:12
【问题描述】:

我知道 log4net 的文档指出,调用者位置信息的记录可能非常缓慢,除非不影响软件的性能,否则不应使用。

在 Windows 10 Fall Creators Update 之前,情况一直如此。这是一个小示例项目。

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />
    </configSections>

    <log4net>
        <appender name="DefaultAppender" type="log4net.Appender.RollingFileAppender">
            <file value="logging.log" />
            <encoding value="utf-8" />
            <appendToFile value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <!--without caller location information-->
                <!--<conversionPattern value="%d | %-5p | %t | %m%n" />-->
                <!--with caller location information-->
                <conversionPattern value="%d | %-5p | %t | %C.%M:%L | %m%n" />
            </layout>
        </appender>
        <root>
          <level value="All" />
          <appender-ref ref="DefaultAppender" />
        </root>
    </log4net>

    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

Program.cs

using System;
using System.Diagnostics;
using log4net;

namespace Log4Net.CSharp
{
    class Program
    {
        private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        static void Main(string[] args)
        {            
            LoggingTest(1000);

            Console.ReadKey();
        }

        private static void LoggingTest(int iterations)
        {
            Stopwatch sw = Stopwatch.StartNew();

            for (int i = 0; i < iterations; i++)
            {
                Log.Info("Some info logging.");
            }

            Console.WriteLine($"Logging of {iterations} iterations took: {sw.ElapsedMilliseconds} ms.");
        }
    }
}

在 Windows 更新 (1709) 之后,调用方位置信息(如 %C %M %L)的性能比没有时差大约 100 倍。问题肯定与更新有关,因为回滚后性能恢复正常。

Windows 更新 (1709) 之前的结果

w/o %C %M %L:记录 1000 次迭代耗时:18 毫秒。
w %C %M %L:记录 1000 次迭代耗时:81 毫秒。

Windows 更新 (1709) 后的结果

w/o %C %M %L:记录 1000 次迭代耗时:14 毫秒。
w %C %M %L:记录 1000 次迭代耗时:1502 毫秒。

任何人都可以确认这个问题或知道发生了什么吗?

感谢任何有关如何调试/修复它的建议。提前致谢!

【问题讨论】:

标签: c# performance windows-10 log4net system.diagnostics


【解决方案1】:

Microsoft 已经更新了几天前提到的文章 (https://support.microsoft.com/en-us/help/4057154/performance-of-system-diagnostics-stackframe-degrades-in-windows-10-17),正如 Jeeze 在评论中所说,新的更新 (https://support.microsoft.com/en-us/help/4058258) 解决了独立于所使用的 .NET Framework 的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 2018-07-30
    • 2017-09-27
    相关资源
    最近更新 更多