如何配置log4net consoleappender以根据Level写入Console.Err和Console.Out?

时间:2014-03-12 20:26:37

标签: c# .net vb.net log4net

当我在 logger.Warn 下执行任何操作时,我想写信给 Console.Out ,然后我想写信给 Console.Err 当我记录任何 logger.Error 及以上。我该如何编写我的log4net配置文件?

到目前为止,我有:

<?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>

1 个答案:

答案 0 :(得分:14)

您可以创建两个appender,并以不同方式配置它们。像(未经测试)的东西:

<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>

编辑:更正了levelMinlevelMax之间的逻辑错误。