默认log4net配置

时间:2012-11-15 11:18:47

标签: c# asp.net web-config log4net

我有以下代码:

    public Log4NetLogger()
        : base(LogLevel.Debug)
    {
        if(!LogManager.GetRepository().Configured)
        {
            lock(mLocker)
            {
                if(!LogManager.GetRepository().Configured)
                {
                    XmlConfigurator.Configure();
                }
            }
        }

        mLog4NetInstance = LogManager.GetLogger(typeof(Log4NetLogger));
    }

使用以下web.config:

  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Logs\MaMLog_" />
      <appendToFile value="true" />
      <StaticLogFileAppName value="false" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMMdd_dddd'.log'" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{dd MMM yyyy HH:mm:ss,fff} [%thread] -() %message%newline" />
      </layout>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    </appender>
    <logger name="MyLog1">
      <level value="Debug" />
      <appender-ref ref="RollingLogFileAppender" />
    </logger>
    <logger name="MyLog2">
      <level value="Debug" />
      <appender-ref ref="RollingLogFileAppender" />
    </logger>
  </log4net>

代码如何知道记录器名称?如果有的话?

1 个答案:

答案 0 :(得分:3)

因为您正在使用

mLog4NetInstance = LogManager.GetLogger(typeof(Log4NetLogger));

然后记录器名称将是typeof(Log4NetLogger).FullName的值,其格式通常为YourNameSpace.Log4NetLogger

此示例中不会记录任何内容,因为您没有具有该名称的记录器,并且您没有根部分可以回退,例如

<root>
    <level value="DEBUG" />
    <appender-ref ref="RollingLogFileAppender" />
</root>