Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure log4net consoleappender to write to Console.Err and Console.Out based on Level?

I would like to write to Console.Out when I do anything below logger.Warn and then I would like to write to Console.Err when I log anything logger.Error and above. How would I write my log4net config file?

So far I have:

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

      <root>
         <level value="DEBUG" />
         <appender-ref ref="ConsoleAppender" />
      </root>
</log4net>
like image 333
Denis Avatar asked Mar 12 '14 20:03

Denis


1 Answers

You can create two appenders, and configure them differently. Something like (untested):

<log4net>
<appender name="ConsoleOutAppender" type="log4net.Appender.ConsoleAppender">
    <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG" />
        <levelMax value="WARN" />
    </filter>       
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="..."  />
    </layout>
</appender>
<appender name="ConsoleErrorAppender" type="log4net.Appender.ConsoleAppender">
    <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="ERROR" />
        <levelMax value="FATAL" />
    </filter>       
    <target value="Console.Error" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="..." />
    </layout>
</appender>
<root>
    <level value="DEBUG" />
    <appender-ref ref="ConsoleOutAppender" />
    <appender-ref ref="ConsoleErrorAppender" />
</root>
</log4net>

EDIT: Corrected logic error with levelMin vs. levelMax.

like image 124
Joe Avatar answered Sep 25 '22 02:09

Joe