1

I'm using Log4Net for logging all different kinds of events in my application. Therefore I configured a fileAppender and a smtpAppender. While the fileAppender is logging everything, the smptAppender should only send me an e-mail if the logging level is FATAL.

Right now, Log4Net is sending me e-mails for both ERROR and FATAL level.

That's my configuration:

<log4net>
  <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="C:\Temp\xxx.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <maxSizeRollBackups value="150" />
    <layout type="log4net.Layout.PatternLayout">
      <header value="DateTime | Thread | Level | ClassName | Message" />
      <conversionPattern value="%date | %thread | %-5level | %logger | %message%newline" />
    </layout>
  </appender>
  <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
    <filter type="log4net.Filter.LevelMatchFilter">
      <acceptOnMatch value="true" />
      <levelToMatch value="FATAL" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    <to value="xxx" />
    <from value="xxx" />
    <subject value="Error" />
    <smtpHost value="xxx" />
    <bufferSize value="512" />
    <lossy value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date | %thread | %-5level | %logger | %message%newline" />
    </layout>
  </appender>
  <root>
    <level value="ALL" />
    <appender-ref ref="FileAppender" />
    <appender-ref ref="SmtpAppender" />
  </root>
</log4net>

I'm using the most recent Log4Net version[1.2.15] 2.0.5.

Curiously, the smptAppender is filtering the INFO and DEBUG level with this configuration.

Using the level evaluator or the level range instead of the level match filter didn't work either.

Level evaluator:

<evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="FATAL"/>
</evaluator>

Level range filter:

<filter type="log4net.Filter.LevelRangeFilter">
   <param name="LevelMin" value="FATAL"/>
   <param name="LevelMax" value="FATAL"/>
</filter>

I know that there are many threads about this topic here on stackOverflow, but none of them could help me with my problem.

Threads with the same topic:

How do I configure a log4net SmtpAppender to only send me e-mails when a certain level is hit?

Log4j2: SMTPAppender does not send mails with error or fatal level

Log4Net LevelEvaluator Ignored when bufferSize greater than 1 for SmtpAppender

Community
  • 1
  • 1
Felix
  • 2,670
  • 3
  • 13
  • 21

1 Answers1

0

well I know that this question is quite old, but I post this answer for others having this problem.

You are using the "lossy" and "bufferSize" settings. I guess that it is to get the last 512 events (not only the FATAL events) when the log is reaching a FATAL event.

Instead of using a LevelMatchFilter or a LevelEvaluator, can you simply test with a simple "threshold" (not in an evaluator).

See Why do Log4Net Filters Receive Messages Outside of the Evaluator Threshold? for more information.

But I think that in this case you will not have other events in the mail that you will receive, so the buffering would be useless.