Log4Net Multiple loggers

北城以北 提交于 2019-12-05 00:44:04
XMight

You have several typos on your example. First is you don't close the configuration tag, and why you're getting only in one file, is because you call:

logger.Info("started async");

and after that you surprisingly do:

logger.Info("started sync");

You will not get writing in the second file because you actually don't log to it.

And in console I get from your code:

Logger: Async
Appenders: FileInfoAppenderS, FileInfoAppenderA
Logger: Sync
Appenders: FileInfoAppenderS, FileInfoAppenderA

For future information, you did correctly by putting additivity to false, because this means that the loggers will not inherit from root logger. As about the statement:

I want to have 2 loggers with different file appenders and restrict each to write into root logger

I do not understand it. If you want that your loggers write to these files while root logger having a console appender for example, just remove additivity and they will write to console and their own files. Also tested and it works very well.

I have read your comment. Now I add the code that I'm using and getting what you need:

class Program
{
    static void Main(string[] args)
    {
        log4net.Config.XmlConfigurator.Configure();

        ILog logger = LogManager.GetLogger("Async");
        logger.Info("started async");
        Console.WriteLine("Logger: {0}", (logger as log4net.Core.LogImpl).Logger.Name);
        Console.WriteLine("Appenders: {0}", string.Join(", ", (logger as log4net.Core.LogImpl).Logger.Repository.GetAppenders().ToList().Select(appendr => appendr.Name)));


        ILog logger2 = LogManager.GetLogger("Sync");
        logger2.Info("started sync");
        Console.WriteLine("Logger: {0}", (logger2 as log4net.Core.LogImpl).Logger.Name);
        Console.WriteLine("Appenders: {0}", string.Join(", ", (logger2 as log4net.Core.LogImpl).Logger.Repository.GetAppenders().ToList().Select(appendr => appendr.Name)));

        Console.ReadKey();
    }
}

And the app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <log4net>
    <appender name="FileInfoAppenderA" type="log4net.Appender.RollingFileAppender">
      <file value="C:\\temp\\AsyncTest.log"/>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG"/>
        <levelMax value="FATAL"/>
      </filter>
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="100MB"/>
      <staticLogFileName value="true"/>
      <datePattern value="yyyyMMdd"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d - %m%n"/>
      </layout>
    </appender>

      <appender name="FileInfoAppenderS" type="log4net.Appender.RollingFileAppender">
        <file value="C:\\temp\\SyncTest.log"/>
        <filter type="log4net.Filter.LevelRangeFilter">
          <levelMin value="DEBUG"/>
          <levelMax value="FATAL"/>
        </filter>
        <appendToFile value="true"/>
        <rollingStyle value="Size"/>
        <maxSizeRollBackups value="10"/>
        <maximumFileSize value="100MB"/>
        <staticLogFileName value="true"/>
        <datePattern value="yyyyMMdd"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%d - %m%n"/>
        </layout>
      </appender>

    <root>
      <level value="INFO"/>
    </root>

    <logger name="Sync" additivity="false">
      <level value="INFO"/>
      <appender-ref ref="FileInfoAppenderS"/>
    </logger>

    <logger name="Async" additivity="false">
      <level value="INFO"/>
      <appender-ref ref="FileInfoAppenderA"/>
    </logger>
  </log4net>
</configuration>

Log4net version: 1.2.13.0 with .NET 4.0 Tell me please if you get what you want.

This configuration worked for me:

<log4net>
<root name="EventLog">
<level value="ALL"/>
<appender-ref ref="EventLogAppender"/>
</root>

<logger name="FileLogger" additivity="false">
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />

...appenders
</logger>

Good Luck!

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