我知道 log4net 的文档指出,记录调用者位置信息可能非常慢,除非软件的性能不受影响,否则不应使用。
在 Windows 10 秋季创意者更新之前,情况都是如此。这是一个小示例项目。
应用程序配置
<?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>
程序.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 毫秒。
任何人都可以确认这个问题或者知道发生了什么吗?
我很感激任何关于如何调试/修复它的建议。提前致谢!