Log4Net LevelEvaluator Ignored when bufferSize greater than 1 for SmtpAppender

心已入冬 提交于 2019-11-30 23:13:36

问题


I have configured log4net with a RollingLogFileAppender and a SmtpAppender, with the intention of logging the DEBUG level to the RollingLogFileAppender and FATAL only to the SmtpAppender:

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
  <to value="test@test.com" />
  <from value="test@test.com" />
  <subject value="Fatal Error" />
  <smtpHost value="smtp.test.com" />
  <SMTPPort value="366"/>
  <Username value="test@test.com"/>
  <Password value="password"/>      
  <bufferSize value="1" />
  <lossy value="true" />
  <evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="FATAL"/>
  </evaluator>      
  <layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline"                             />
  </layout>
</appender>

<root>
  <level value="DEBUG" />
  <appender-ref ref="RollingLogFileAppender" />
  <appender-ref ref="SmtpAppender" />
</root>

This works perfectly until I increase the bufferSize. When I do this, all levels are sent via email and the log4net.Core.LevelEvaluator seems to be ignored. I have also tried using LevelRangeFilter and LevelMatchFilter but with these configured I seem to get no emails at all.


回答1:


The evaluator is not ignored, but it does not do what you expect: Your settings instruct the appender to put all log messages on a buffer and send an email only when a message with level FATAL is logged. If the buffer is full then the oldest messages are discarded (that is the lossy setting; without it you would also get an email as soon as the buffer is full).

If you want to filter the messages then you need to use a filter. For instance like this:

<filter type="log4net.Filter.LevelMatchFilter">
   <acceptOnMatch value="true" />
   <levelToMatch  value="FATAL" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />

I am not sure though if I would consider my mail appender like this since I would want to get notified immediately if my application has a problem that it needs to log it with level FATAL.



来源:https://stackoverflow.com/questions/13254495/log4net-levelevaluator-ignored-when-buffersize-greater-than-1-for-smtpappender

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!