log4net的SMTP附加器只发送电子邮件时具有完全日志错误(调试和放大器;信息&安培;错误)。只有当应用程序结束安培、错误、放大器、应用程序

2023-09-03 09:41:23 作者:告白讯息

我想配置,我有log4net.config文件中的SMTP附加目的地。问题是,我期待所有在互联网上,无法找到如何发生错误时与其他所有的日志信息,包括如信息,调试,错误,致命发送电子邮件。只有当应用程序结束(并不是每个错误发生的时间)。

I am trying to configure a smtp appender in the log4net.config file that I have. The problem is that I have looked all over the internet and cannot find how to send an email when an error occurs with all the other log information included such as info, debug, error, fatal. Only when the application ends (NOT every time an ERROR occurs).

所以,我只希望收到此电子邮件时:   该应用程序端+   与所有的日志信息(DEBUG,INFO,ERROR,FATAL)+   只有当发生错误。

So I only want to receive this email when: The application ends + With all the log information (DEBUG, INFO, ERROR, FATAL) + Only if an ERROR has occured.

在阐述一些,这是因为这样,我处理我的例外升C,具有多级处理所有的地方,因此,如果不管有多少次我只是想收到一封电子邮件中出现错误。此外,我不希望使用多个日志,而是 只有一个根。

Elaborating some more this is because of the way I handle my exceptions in c sharp, with multiple level handling all over the place and so if an error occurs no matter how many times I only want to receive one email. Also I do not want to use multiple logs, but rather just one in root.

感谢。

推荐答案

SmtpAppender 无法自己做到这一点。所以我所做的就是创建另一个附加器,为类型的附加器 MemoryAppender 。我对这个记录器设置一个门槛,只包括应触发 SmtpAppender ,例如消息错误。我们用它来确定以后,如果我们想送它有多个级别记录电子邮件。

SmtpAppender cannot accomplish this on its own. So what I did was create another appender that to an appender of type MemoryAppender. I set a threshold on this logger to only include messages that should trigger the SmtpAppender, e.g. Error. We use this to later determine if we want to send the email which has more levels logged.

我们并不真正关心消息中的 MemoryAppender - 我们只关心它包含在最终的消息。通过电子邮件,我们得到的消息实际上来自 SmtpAppender

We don't actually care about the messages in the MemoryAppender--we just care that it contains messages at the end. The messages we get via email actually come from the SmtpAppender.

在我的程序结束时,我检查内存附加器,看看它GetEvents()中包含的任何事件。如果是这样,我的不的停止 SmtpAppender 从正常运行。

At the end of my program I check the memory appender to see if its GetEvents() contains any events. If so, I do not stop the SmtpAppender from running normally.

log4net的两个附加目的地CONFIGS:

Log4Net configs for both appenders:

<appender name="ErrorHolder" type="log4net.Appender.MemoryAppender" >
    <onlyFixPartialEventData value="true" />
    <!-- if *any* message is logged with this level, the email appender will 
         be used with its own level -->
    <threshold value="ERROR" />
</appender>

<appender name="Email" type="log4net.Appender.SmtpAppender">    
    <!-- the level you want to see in the email IF ErrorHolder finds anything -->
    <threshold value="INFO"/> 
    <bufferSize value="512" />
    <lossy value="false" /> <!-- important! -->   
    <to value="name@domain.com" />
    <from value="name@domain.com" />
    <subject value="ERROR: subject" />
    <smtpHost value="smtpserver" />    
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
    </layout>    
</appender>

<root>
  <level value="ALL" />
  <appender-ref ref="ErrorHolder" />
  <appender-ref ref="Email" />
</root>

在应用程序的结束时运行该禁用 SmtpAppender 如果 ErrorHolder 附加器是空的:

At the end of the app run this to disable the SmtpAppender if the ErrorHolder appender is empty:

// trigger loggers if errors occurred:
var memoryAppender = ((Hierarchy)LogManager.GetRepository())
    .Root.Appenders.OfType<MemoryAppender>().FirstOrDefault();

if (memoryAppender != null && memoryAppender.GetEvents().Length == 0)
{
    // there was no error so don't email anything
    var smtpAppender = ((Hierarchy)LogManager.GetRepository())
        .Root.Appenders.OfType<SmtpAppender>().FirstOrDefault();

    if (smtpAppender != null)
    {
        smtpAppender.Threshold = Level.Off;
        smtpAppender.ActivateOptions();
    }
}