Does Log4net output SMTPAppender asynchronously via email?

Does Log4net send SMTPAppenderasynchronous email? If this is not the case, how can I send email logs asynchronously?

My log4net.config:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <log4net>
    <appender name="SMTPAppender" type="log4net.Appender.SMTPAppender">
      <authentication value="Basic" />
      <to value="xxx@xx.com" />
      <from value="yyy@xx.com" />
      <username value="yyy@xx.com" />
      <password value="yyy" />
      <subject value="xxx" />
      <smtpHost value="smtp.xx.com" />
      <lossy value="true" />
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="WARN" />
      </evaluator>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date [%thread] %-5level %logger %newline %message%newline%newline%newline" />
      </layout>
    </appender>
    <root>
      <level value="INFO"></level>
    </root>
    <logger name="MyLogger">
      <level value="INFO"></level>
      <appender-ref ref="SMTPAppender"></appender-ref>
    </logger>
  </log4net>
</configuration>
+5
source share
3 answers

You can simply call the logging method asynchronously as follows:

Task.Factory.StartNew(() => log.Info("Message I want to email"));

I really got this offer from the following SO article:

How to create an asynchronous shell for log4net?

+3
source

There is a simple solution to the problem before a solid solution is released.

public class SmtpAsyncAppender : SmtpAppender
{
    protected override void SendEmail(string messageBody)
    {
        Task.Run(() => base.SendEmail(messageBody));
    }
}

, SmtpAppender.SendEmail . log4net v1.2.13.0, .

+2

Log4net is not built into the appender, which is asynchronous. If you need this functionality, you need to write your own appender. Download the source code for log4net to get you started ...

0
source

All Articles