如何阻止log4net写入两个单独的文件

时间:2010-08-26 19:30:46

标签: .net log4net log4net-configuration

我正在使用以下log4net配置。我正在尝试将性能日志写入单独的文件,但输出将转到这两个文件。即使我正在指定:

private static readonly log4net.ILog log = log4net.LogManager.GetLogger("PerformanceMetricsLogger");

在这种情况下,如何阻止log4net写入默认的appender?

<log4net>
  <!-- Define some output appenders -->
  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">    
    <file type="log4net.Util.PatternString" value="c:\ilap\IlapAdServerLog_%property{log4net:HostName}.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="-1" />
    <maximumFileSize value="2000KB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>

  <appender name="RollingFileAppenderForPerformance" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="c:\ilap\IlapAdServerLog_%property{log4net:HostName}.peformance.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="-1" />
    <maximumFileSize value="2000KB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>

  <!-- Setup the root category, add the appenders and set the default level -->
  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingFileAppender" />
  </root>

  <logger name="PerformanceMetricsLogger">
    <level value="DEBUG" />
    <appender-ref ref="RollingFileAppenderForPerformance" />
  </logger>
</log4net>

1 个答案:

答案 0 :(得分:7)

root下面的所有记录器级别都会从根记录器继承appender。这就是您的消息被记录两次的原因。要禁用记录器的继承,请将其additivity参数设置为false:

 <logger additivity="false" name="PerformanceMetricsLogger">
    <level value="DEBUG" />
    <appender-ref ref="RollingFileAppenderForPerformance" />
  </logger>