2010-08-26 76 views
5

我正在使用下面的log4net配置。我正在嘗試將性能日誌寫入一個單獨的文件,但輸出將轉到兩個文件。即使我指定:如何停止log4net寫入兩個單獨的文件

private static readonly log4net.ILog log = log4net.LogManager.GetLogger("PerformanceMetricsLogger"); 

在這種情況下,如何阻止log4net寫入默認appender?

<log4net> 
    <!-- Define some output appenders --> 
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">  
    <file type="log4net.Util.PatternString" value="c:\ilap\IlapAdServerLog_%property{log4net:HostName}.txt" /> 
    <appendToFile value="true" /> 
    <rollingStyle value="Size" /> 
    <maxSizeRollBackups value="-1" /> 
    <maximumFileSize value="2000KB" /> 
    <staticLogFileName value="true" /> 
    <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> 
    </layout> 
    </appender> 

    <appender name="RollingFileAppenderForPerformance" type="log4net.Appender.RollingFileAppender"> 
    <file type="log4net.Util.PatternString" value="c:\ilap\IlapAdServerLog_%property{log4net:HostName}.peformance.log" /> 
    <appendToFile value="true" /> 
    <rollingStyle value="Size" /> 
    <maxSizeRollBackups value="-1" /> 
    <maximumFileSize value="2000KB" /> 
    <staticLogFileName value="true" /> 
    <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> 
    </layout> 
    </appender> 

    <!-- Setup the root category, add the appenders and set the default level --> 
    <root> 
    <level value="DEBUG" /> 
    <appender-ref ref="RollingFileAppender" /> 
    </root> 

    <logger name="PerformanceMetricsLogger"> 
    <level value="DEBUG" /> 
    <appender-ref ref="RollingFileAppenderForPerformance" /> 
    </logger> 
</log4net> 

回答

7

根以下的所有記錄器級別從根記錄器繼承appender。這就是爲什麼你的消息記錄了兩次。要禁用記錄器的繼承,請將其參數additivity設置爲false:

<logger additivity="false" name="PerformanceMetricsLogger"> 
    <level value="DEBUG" /> 
    <appender-ref ref="RollingFileAppenderForPerformance" /> 
    </logger> 
相關問題