views:

157

answers:

1

Hi,

I haven't quite worked this one out yet - how do I set DEBUG for my Console output, and INFO for my FileAppender? That is, how would I change the configuration below so that I can get the ConsoleAppender pumping out all logging from DEBUG upwards? Note that both appenders need to cover all logging (i.e. I don't want to use a scoped logger element that changes the log level for just one logger name)

  <log4net>

    <root>
      <level value="INFO" />
      <appender-ref ref="LogFileAppender" />
      <appender-ref ref="ConsoleAppender" />
    </root>

    <appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
      <param name="File" value="log-file.txt" />
      <param name="AppendToFile" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="[Header]\r\n"/>
        <param name="Footer" value="[Footer]\r\n"/>
        <param name="ConversionPattern"
           value="%d [%t] %-5p %c [%x] - %m%n"
        />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
        <param name="LevelMax" value="WARN" />
      </filter>
    </appender>

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
      </layout>
    </appender>

  </log4net>
+3  A: 

Try this configuration:

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

<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
  <param name="File" value="log-file.txt" />
  <param name="AppendToFile" value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <param name="Header" value="[Header]\r\n"/>
    <param name="Footer" value="[Footer]\r\n"/>
    <param name="ConversionPattern"
       value="%d [%t] %-5p %c [%x] - %m%n"
    />
  </layout>
  <filter type="log4net.Filter.LevelRangeFilter">
    <param name="LevelMin" value="INFO" />
    <param name="LevelMax" value="FATAL" />
  </filter>
</appender>

<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
  </layout>
</appender>

Note:

  • I set the may level of the file appender to Fatal so that error are also written to your file. If that is not what you want you can change it easily back.
  • If you want to have only DEBUG (i.e. no info, warn...) messages in the console you need to filter the console appender as well.
Stefan Egli