自MinimalLock设置

时间:2016-11-18 10:09:13

标签: log4net log4net-configuration log4net-appender

我最近不得不在我的C#程序中遇到一些性能问题,并发现log4net是这些问题的根源。我更改了添加MinimalLock的配置,这确实有很大的帮助。

这就是我现在配置log4net的方式:

var hierarchy = (Hierarchy)log4net.LogManager.GetRepository();

var patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
patternLayout.ActivateOptions();

var roller = new RollingFileAppender();

roller.LockingModel = new FileAppender.MinimalLock();
roller.LockingModel.ActivateOptions();

roller.Encoding = System.Text.Encoding.UTF8;
roller.AppendToFile = true;
roller.File = Path.Combine(Logger.LogPath, "log.txt");
roller.Layout = patternLayout;
roller.MaxSizeRollBackups = 5;
roller.MaximumFileSize = "100MB";
roller.RollingStyle = RollingFileAppender.RollingMode.Size;
roller.StaticLogFileName = true;
roller.ActivateOptions();

hierarchy.Root.AddAppender(roller);
hierarchy.Root.Level = Level.Debug;
hierarchy.Configured = true;

这样做的缺点是现在我的日志包含了多次重复的相同日志行。

我怀疑我误解了log4net配置中的某些内容,现在我要输出的每个日志行都会像应用程序的进程一样多次发送(显然,创建的其余进程没有记录任何内容,或者至少同时不是同一条线!)

这是一个日志行的例子:

  

2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - 使用config读取dat:manuel.dat:无   2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol
  2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol
  2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol
  2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol
  2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol
  2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol
  2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol
  2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol
  2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol
  2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol
  2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol
  2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol
  2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol
  2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol
  2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol
  2016-11-18 10:35:34,512 [1] INFO ViewModel - 运行模式:Lol

1 个答案:

答案 0 :(得分:2)

在你的情况下:
如果消息全部在同一时间和相同的线程上,那么您的某个组件可能正在添加跟踪侦听器。 检查System.Diagnostics.Trace.Listeners属性 在我的情况下,Owin Server在启动时添加了它们: OWIN interferes with log4net

对于其他人:
消除由多个appender引起的可能性 您可以使用LogManager.GetRepository().GetAppenders().Length;了解 请记住,您的记录器将继承根记录器的追加器。

确保您的代码只被调用一次 (您可以通过在conversationPattern中添加[%thread]来打印线程ID。

<layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%date [%thread] %message%newline" />
  </layout>