log4net同时记录到两个文件

时间:2013-11-07 08:16:10

标签: asp.net-mvc log4net

我有两个记录器,一个叫做NotificationLogger,另一个叫做VMPLogger。出于某种原因,无论我调用哪个记录器,它都会写入两个日志!

我的web.config

  <log4net>
    <appender name="VMPLogger" type="log4net.Appender.RollingFileAppender">
      <file value="logs\vmp_" />
      <appendToFile value="true" />
      <staticLogFileName value="false" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMM'.log'" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <appender name="NotificationLogger" type="log4net.Appender.RollingFileAppender">
      <file value="logs\notifications_" />
      <staticLogFileName value="false" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMM'.log'" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="VMPLogger" />
      <appender-ref ref="NotificationLogger" />
    </root>
  </log4net>

我的公共记录器(在global.asax中)

    public static readonly ILog VmpLogger = LogManager.GetLogger("VMPLogger");
    public static readonly ILog NotificationLogger = LogManager.GetLogger("NotificationLogger");

    protected void Application_Start()
    {
        XmlConfigurator.Configure();
    }

我的测试控制器操作

    public void LogSomethingNotification()
    {
        MvcApplication.NotificationLogger.Info("Something");
    }

任何人都可以从这个代码/配置中看到原因吗?感谢

1 个答案:

答案 0 :(得分:1)

因为GetLogger没有获得appender,而是一个特定的记录器。您需要在appender中过滤您想要查看的记录器。在您的appender中添加过滤器以仅记录您的一个记录器:

<log4net>
<appender name="VMPLogger" type="log4net.Appender.RollingFileAppender">
  <file value="logs\vmp_" />
  <appendToFile value="true" />
  <staticLogFileName value="false" />
  <rollingStyle value="Date" />
  <datePattern value="yyyyMM'.log'" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  </layout>
  <filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="VMPLogger" />
  </filter>     
  <filter type="log4net.Filter.DenyAllFilter" />
</appender>
<appender name="NotificationLogger" type="log4net.Appender.RollingFileAppender">
  <file value="logs\notifications_" />
  <staticLogFileName value="false" />
  <appendToFile value="true" />
  <rollingStyle value="Date" />
  <datePattern value="yyyyMM'.log'" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  </layout>
  <filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="NotificationLogger" />
  </filter>     
  <filter type="log4net.Filter.DenyAllFilter" />
</appender>
<root>
  <level value="ALL" />
  <appender-ref ref="VMPLogger" />
  <appender-ref ref="NotificationLogger" />
</root>