2016-02-09 217 views
0

問題:所有記錄的信息在文件中寫入兩次。 請查看我使用的以下配置: 只有引入多個記錄器時,我才能觀察到此問題。windsor castle - log4net - RollingFileAppender - 問題 - 信息在文件中寫入兩次

<root> 
     <level value="DEBUG" /> 
     <appender-ref ref="LogFileAppender" /> 
     <appender-ref ref="MemoryAppender"/> 
     <appender-ref ref="EventsFileAppender" /> 
    </root> 
    <logger name="FileLogger"> 
     <level value="DEBUG" /> 
     <appender-ref ref="LogFileAppender" /> 
     <appender-ref ref="MemoryAppender"/> 
    </logger> 
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" > 
     <param name="File" value="C:\log-file.txt" /> 
     <param name="AppendToFile" value="true" /> 
     <rollingStyle value="Size" /> 
     <maxSizeRollBackups value="10" /> 
     <maximumFileSize value="5MB" /> 
     <staticLogFileName value="true" /> 
     <filter type="log4net.Filter.LoggerMatchFilter"> 
      <LoggerToMatch value="FileLogger" /> 
     </filter> 
     <filter type="log4net.Filter.DenyAllFilter" /> 
     <layout type="log4net.Layout.PatternLayout"> 
      <param name="ConversionPattern" value="%date [%thread] %-5level - %message%newline" /> 
     </layout> 
    </appender> 
<appender name="MemoryAppender" type="log4net.Appender.MemoryAppender"> 
    <filter type="log4net.Filter.LoggerMatchFilter"> 
     <LoggerToMatch value="FileLogger" /> 
    </filter> 
    <filter type="log4net.Filter.DenyAllFilter" /> 
</appender> 

<logger name="EventsLogger"> 
    <level value="DEBUG" /> 
    <appender-ref ref="EventsFileAppender" /> 
</logger>  
<appender name="EventsFileAppender" type="log4net.Appender.RollingFileAppender" > 
    <param name="File" value="C:\ObjectServerEvents\ObjectServerEvents-file.txt" /> 
    <param name="AppendToFile" value="true" /> 
    <rollingStyle value="Size" /> 
    <maxSizeRollBackups value="10" /> 
    <maximumFileSize value="100MB" /> 
    <staticLogFileName value="true" /> 
    <filter type="log4net.Filter.LoggerMatchFilter"> 
     <LoggerToMatch value="EventsLogger" /> 
    </filter> 
    <filter type="log4net.Filter.DenyAllFilter" /> 
    <layout type="log4net.Layout.PatternLayout"> 
     <param name="ConversionPattern" value="%date [%thread] %-5level - %message%newline" /> 
    </layout> 
</appender> 

你能發現這個錯誤嗎?如何解決這個問題?

回答

1

您正在兩次添加appender:一次到根元素,一次到每個記錄器。

根元素或記錄器中取出附加器的聲明,和每個事件將只記錄一次。

<root> 
    <level value="DEBUG" /> 
    <appender-ref ref="LogFileAppender" /> 
    <appender-ref ref="MemoryAppender"/> 
    <appender-ref ref="EventsFileAppender" /> 
</root> 
<logger name="FileLogger"> 
    <level value="DEBUG" />  
</logger> <!-- etc -->