如何通过记录器改变appender的行为

时间:2014-02-24 21:10:22

标签: c# log4net log4net-configuration

我有一个调用存储过程的appender来写日志记录。它看起来像这样(为简洁而修剪):

<log4net>
  <root>
    <level value="WARN" />
    <appender-ref ref="ElsAppender" />
  </root>
  <appender name="LogAppender" type="LoggerAssembly.Appender, LoggerAssembly">
    <bufferSize value="1" />
    <reconnectonerror value="True" />
    <parameter>
      <parameterName value="@EntryDate" />
      <dbType value="DateTime" />
      <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
      <parameterName value="@message" />
      <dbType value="String" />
      <size value="40000" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message" />
      </layout>
    </parameter>
  </appender>
</log4net>

请注意,message参数的size字段设置为40,000。

我有一个记录器但是我需要消息长度更长。如何更改该特定记录器的此设置。我尝试克隆LogAppender并更改大小值,如下所示:

<log4net>
  <root>
    <level value="WARN" />
    <appender-ref ref="LogAppender" />
  </root>
  <logger name="SpecialLogger">
    <level value="INFO" />
    <appender-ref ref="SpecialLogAppender" />
  </logger>
  <appender name="LogAppender" type="LoggerAssembly.Appender, LoggerAssembly">
    <bufferSize value="1" />
    <reconnectonerror value="True" />
    <parameter>
      <parameterName value="@EntryDate" />
      <dbType value="DateTime" />
      <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
      <parameterName value="@message" />
      <dbType value="String" />
      <size value="40000" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message" />
      </layout>
    </parameter>
  </appender>
  <appender name="SpecialLogAppender" type="LoggerAssembly.Appender, LoggerAssembly">
    <bufferSize value="1" />
    <reconnectonerror value="True" />
    <parameter>
      <parameterName value="@EntryDate" />
      <dbType value="DateTime" />
      <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
      <parameterName value="@message" />
      <dbType value="String" />
      <size value="400000" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message" />
      </layout>
    </parameter>
  </appender>
</log4net>

但是这会导致来自SpecialLogger的消息被记录两次,一次由LogAppender记录,一次由SpecialLogAppender记录。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您需要将additivity="false"添加到特殊记录器定义中:

<logger name="SpecialLogger" additivity="false">
  <level value="INFO" />
  <appender-ref ref="SpecialLogAppender" />
</logger>

答案 1 :(得分:1)

您需要在log4net appender中添加合适的<filter>元素。见

按文档顺序检查Log4net appender,因此您的特殊appender需要在默认appender之前。那么这样的事情应该做到了:

<appender name="SpecialAppender" type="log4net.Appender.FileAppender">
  <file value="log.txt" />
  <filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="your-logger-name-here">
    <acceptOnMatch="true"
  </filter>
  <layout type="log4net.Layout.SimpleLayout" />
</appender>
<appender name="DefaultAppender" type="log4net.Appender.FileAppender">
  <file value="log.txt" />
  <filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="your-logger-name-here">
    <acceptOnMatch="false"
  </filter>
  <layout type="log4net.Layout.SimpleLayout" />
</appender>