0

I am using log4net.Appender.SmtpAppender in application. This is how it is configured.

  <appender name="FatalSmtpAppender" type="log4net.Appender.SmtpAppender">
  <to value="...." />
  <from value="......" />
  <subject value="URGENT: Error occured" />
  <smtpHost value="....." />
  <bufferSize value="1" />
  <lossy value="false" />
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="FATAL" />
    <levelMax value="FATAL" />
  </filter>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
  </layout>
</appender>

It is working as expected so far.

Now there is change in business requirement. If there is an error, system will try to fix 3 times. After all attempts are failed, it should send email.

3 attempts to fix error part is coded. But the log4net sends email on first occurrence of error. I don't know how to hold email until 3 attempts.

I hope it is not confusing. Thanks!!!

gmail user
  • 2,753
  • 4
  • 33
  • 42
  • If you have logic to count the number of attempts, then you should only call the log4net log command whenever it has exceeded that number. – sgmoore May 15 '15 at 19:14

1 Answers1

0

The logic of sending a error after 3 attempts is best solved in the business logic not in an smtp appender.

int failurecount = 0;
while (true){   
...
  try{
  ... 
  }catch(...){
      failurecount++;
      if (failurecount == 3){
          _logger.Error(...);
           break;
      } // else log warning
  }
}

Appenders do not know how error relate to each other and you have to keep in mind that log4net is a best-effort and fail-stop logging system. You will not be informed if log4net fails to log. See https://logging.apache.org/log4net/release/faq.html for more information.

Peter
  • 27,590
  • 8
  • 64
  • 84