Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 691
Jen Avatar asked Jun 14 '11 16:06

Jen


People also ask

Does log4net support structured logging?

log4net doesn't support the concept of structured logging. Like shown in the conversionPattern element in the XML configuration, you have some variables to play with when writing to the storage. But including properties like FirstName in the Serilog example isn't available.

What is level value in log4net?

log4net offers the following log levels, in increasing order of priority: ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF. The ALL level logs everything and the OFF level logs nothing.


1 Answers

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.

like image 117
gregwhitaker Avatar answered Sep 30 '22 04:09

gregwhitaker