How to log different log level to different log appender in log4net

Jen picture Jen · Jun 14, 2011 · Viewed 25.7k times · Source

I have a WinForm that it display log information but I want it to display to only INFO level but I want the log in the log file to contain DEBUG level as well.

Below is my configuration:

<?xml version="1.0" encoding="utf-8" ?>
<!-- This section contains the log4net configuration settings -->
<log4net>
  <!-- Define some output appenders -->

  <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="${ALLUSERSPROFILE}\\Application Data\\logs\\Gateway" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <param name="StaticLogFileName" value="false" />
    <datePattern value=".yyyyMMdd.lo\g" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date | [%thread] | %-5level | %logger | %message %newline" />
    </layout>
  </appender>

  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>

  <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
    <mapping>
      <level value="DEBUG" />
      <backColor value="Blue" />
    </mapping>
    <mapping>
      <level value="INFO" />
      <backColor value="Green" />
    </mapping>
    <mapping>
      <level value="WARN" />
      <backColor value="Yellow" />
    </mapping>
    <mapping>
      <level value="ERROR" />
      <backColor value="Red" />
    </mapping>
    <mapping>
      <level value="FATAL" />
      <backColor value="Red, HighIntensity" />
    </mapping>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>

  <appender name="RichTextBoxAppender" type="Gateway.RichTextBoxAppender,Gateway" >
    <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>
    <mapping>
      <level value="DEBUG" />
      <textColorName value="DarkGreen" />
    </mapping>
    <mapping>
      <level value="INFO" />
      <textColorName value="ControlText" />
    </mapping>
    <mapping>
      <level value="WARN" />
      <textColorName value="Blue" />
    </mapping>
    <mapping>
      <level value="ERROR" />
      <textColorName value="Red" />
      <bold value="true" />
      <pointSize value="10" />
    </mapping>
    <mapping>
      <level value="FATAL" />
      <textColorName value="Black" />
      <backColorName value="Red" />
      <bold value="true" />
      <pointSize value="12" />
      <fontFamilyName value="Lucida Console" />
    </mapping>
  </appender>


  <!-- Setup the root category, add the appenders and set the default level -->
  <root>
    <level value="WARN" />
    <appender-ref ref="LogFileAppender" />
    <appender-ref ref="ColoredConsoleAppender" />
    <appender-ref ref="RichTextBoxAppender" />
  </root>

  <logger name ="Gateway" additivity="false">
    <level value="INFO" />
    <appender-ref ref="LogFileAppender" />
    <appender-ref ref="ColoredConsoleAppender" />
    <appender-ref ref="RichTextBoxAppender" />
  </logger>



</log4net>

Right now it is set to INFO for both LogFileAppender and RichTextBoxAppender. I tried taking out the LogFileAppender in Gateway logger and move to another logger and set to "DEBUG" level for this new logger but it is still doing one or the other. How do I configure it so that the LogFileAppender has DEBUG level and RichTextBoxAppender has INFO level?

Answer

gregwhitaker picture gregwhitaker · Jun 14, 2011

You should be able to add a filter to both of your appenders.

<filter type="log4net.Filter.LevelRangeFilter">
         <levelMin value="INFO" />
         <levelMax value="FATAL" />
</filter>

This way one appender will only log to a certain level while the other to a different level even though they are defined by the same logger.