2014-03-12 104 views
7

我想,當我做下面logger.Warn什麼寫Console.Out,然後我想寫信給Console.Err當我登錄任何logger.Error以上。我將如何編寫我的log4net配置文件?如何將log4net consoleappender配置爲基於Level寫入Console.Err和Console.Out?

到目前爲止,我有:

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

爲什麼-1投票? – Denis

回答

12

您可以創建兩個附加目的地,以及不同的配置。類似的信息(未測試):

<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:校正的邏輯錯誤與levelMinlevelMax對比。

+0

真棒,謝謝! – Denis