Log4net - Logging to wrong appender

≯℡__Kan透↙ 提交于 2021-02-05 06:52:10

问题


I'm trying to have 2 log files, with my application logging to either by specifying a logger name in code.

The problem is it never logs to the file I'm trying to target, instead it always logs to the last declared appender in the config (LogB in this case).

I have my config set up as follows:

  <log4net>
    <logger name="LogA">
      <appender-ref ref="LogA"/>
      <level value="DEBUG"/>
    </logger>
    <logger name="LogB">
      <appender-ref ref="LogB"/>
      <level value="DEBUG"/>
    </logger>
    <appender name="LogA" type="log4net.Appender.FileAppender">
      <file value="E:\Logs\LogA.log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <appender name="LogB" type="log4net.Appender.FileAppender">
      <file value="E:\Logs\LogB.log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogA" />
    </root>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogB" />
    </root>
  </log4net>

Then in my code when I create a logger:

private static readonly ILog logA = LogManager.GetLogger("LogA");
log.Debug("This should log to LogA");

And in some other class:

private static readonly ILog logB = LogManager.GetLogger("LogB");
log.Debug("This should log to LogB");

But nothing is ever logged to the LogA file, it's always output to the LogB file (or whatever happens to appear last in the config).

What is it I'm missing?


回答1:


For some reason it didn't appear to like the multiple root nodes. When I used this setup instead (keeping the appender nodes), it worked:

    <root>
      <level value="ALL" />
      <appender-ref ref="SomeRootAppender" />
    </root>
    <logger additivity="false" name="LogA">
      <level value="DEBUG"/>
      <appender-ref ref="LogA" />
    </logger>
    <logger additivity="false" name="LogB">
      <level value="DEBUG"/>
      <appender-ref ref="LogB" />
    </logger>


来源:https://stackoverflow.com/questions/45234344/log4net-logging-to-wrong-appender

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!