log4net没有写入两个RollingFileAppenders

时间:2018-12-27 04:23:07

标签: c# .net log4net log4net-configuration log4net-appender

这是我的应用程序配置

   <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingFileAppender" />
    </root>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type='log4net.Util.PatternString' value='%property{LogPath}\%property{LogFileName}' />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level [%thread] - %message%newline" />
      </layout>
    </appender>   
    <appender name="PerformanceLogger" type="log4net.Appender.RollingFileAppender">
      <file type='log4net.Util.PatternString' value='%property{LogPath}\%property{PerformanceLogFileName}' />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="1" />
      <maximumFileSize value="2MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level [%thread] - %message%newline" />
      </layout>
    </appender>
    <logger Name="PerformanceLogger" additivity="false">
      <level value="ALL"/>
      <appender-ref ref="PerformanceLogger"/>
    </logger>
  </log4net>

当我尝试获取记录器并对其进行写入时。它只是为PerformanceLogger创建文件,但不会写入文件。

log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
performaceLog = LogManager.GetLogger("PerformanceLogger");

但是我的log.Info("test");可以正常工作,它可以创建文件并对其进行写入。

请问对此有何建议?

1 个答案:

答案 0 :(得分:1)

PerformanceLogger的记录器定义不正确-您使用Name而不是name来定义记录器的名称。将其更改为以下内容,它应该可以正常工作:

...
<logger name="PerformanceLogger" additivity="false">
  <level value="ALL"/>
  <appender-ref ref="PerformanceLogger"/>
</logger>
...