将log4net与单独的配置文件一起使用

时间:2015-06-15 10:41:36

标签: c# log4net

我的AssemblyInfo.cs:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

我改变结构就像你说的那样

(i just change the <file value="..\Logs\\CurrentLog" /> to <file value="D:\Log\Log.txt" />):

<?xml version="1.0" encoding="UTF-8"?>
<log4net>
  <root>
    <level value="ALL" />
    <appender-ref ref="RollingFileAppender"/>
  </root>
  <appender name="RollingLogFileAppender"  type="log4net.Appender.RollingFileAppender">
    <file value="D:\Log\Log.txt" />
    <appendToFile value="true" />
    <datePattern value="yyyyMMdd" />

    <rollingStyle value="Date" />
    <filter type="log4net.Filter.LevelRangeFilter">
      <acceptOnMatch value="true" />

      <levelMin value="INFO" />
      <levelMax value="FATAL" />
    </filter>

    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern
      value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
    </layout>
  </appender>
</log4net>

并定义日志:

private static readonly ILog Log = LogManager.GetLogger("Logger");

但仍然没有创建log.txt文件!

2 个答案:

答案 0 :(得分:1)

实际上,您还没有在配置中声明记录器并且结构错误。 这就是它应该是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<log4net>
    <logger name="Logger">
        <level value="ALL" />
        <appender-ref ref="RollingFileAppender"/>
    </logger>
    <appender name="RollingLogFileAppender"  type="log4net.Appender.RollingFileAppender">
        <file value="..\Logs\\CurrentLog" />
        <appendToFile value="true" />
        <datePattern value="yyyyMMdd" />

        <rollingStyle value="Date" />
        <filter type="log4net.Filter.LevelRangeFilter">
            <acceptOnMatch value="true" />

            <levelMin value="INFO" />
            <levelMax value="FATAL" />
        </filter>

        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern 
            value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
        </layout>
    </appender>
</log4net>

现在你可以这样访问它:

private static readonly ILog Log = LogManager.GetLogger("Logger");

绝对有效。

现在关于你的代码片段。我不确定你的初始化。我没有用这种方式使用log4net:

private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

但是,您可以尝试在root中声明记录器并初始化它:

<?xml version="1.0" encoding="UTF-8"?>
<log4net>
    <root>
        <level value="ALL" />
        <appender-ref ref="RollingFileAppender"/>
    </root>
    <appender name="RollingLogFileAppender"  type="log4net.Appender.RollingFileAppender">
        <file value="..\Logs\\CurrentLog" />
        <appendToFile value="true" />
        <datePattern value="yyyyMMdd" />

        <rollingStyle value="Date" />
        <filter type="log4net.Filter.LevelRangeFilter">
            <acceptOnMatch value="true" />

            <levelMin value="INFO" />
            <levelMax value="FATAL" />
        </filter>

        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern 
            value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
        </layout>
    </appender>
</log4net>

// ...
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

答案 1 :(得分:1)

配置文件中缺少某些内容。

<log4net>
  <logger name="default">
    <level value="ALL"/>
  </logger>

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

  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="..\Logs\\CurrentLog" />
    <appendToFile value="true" />
    <datePattern value="yyyyMMdd" />
    <rollingStyle value="Date" />
    <filter type="log4net.Filter.LevelRangeFilter">
      <acceptOnMatch value="true" />
      <levelMin value="INFO" />
      <levelMax value="FATAL" />
    </filter>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern 
      value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
    </layout>
  </appender>
</log4net>

还有声明:

private static readonly ILog Log = LogManager.GetLogger("default");