【发布时间】: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 毫秒。
任何人都可以确认这个问题或知道发生了什么吗?
感谢任何有关如何调试/修复它的建议。提前致谢!
【问题讨论】:
-
微软知道这个问题。 (connect.microsoft.com/VisualStudio/feedback/details/3143189/…, support.microsoft.com/en-us/help/4057154/…) 在 NeedFileInfo 为 true 的情况下创建 StackTrace 实例会明显变慢。
-
我已经在 1 小时前发布了“相同”的问题,您找到任何解决方法了吗? stackoverflow.com/questions/48297053/…
-
以防万一有人仍然遇到此问题并找到此主题。更新 KB4058258 完全解决了这个问题。 (support.microsoft.com/en-us/help/4057154/…)
标签: c# performance windows-10 log4net system.diagnostics