C# 使用NLog和使用C的控制台应用程序只发送一封包含所有错误的电子邮件#

C# 使用NLog和使用C的控制台应用程序只发送一封包含所有错误的电子邮件#,c#,nlog,C#,Nlog,我只想发送一封包含我从C#控制台应用程序中得到的所有错误的电子邮件 我的目标是: <target xsi:type="File" name="HeelpAdsImport_log" fileName="${basedir}/logs/HeelpAdsImport-${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${callsite:className=true:includeSourcePath=true:method

我只想发送一封包含我从C#控制台应用程序中得到的所有错误的电子邮件

我的目标是:

<target xsi:type="File" name="HeelpAdsImport_log" fileName="${basedir}/logs/HeelpAdsImport-${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${callsite:className=true:includeSourcePath=true:methodName=true} ${message}" />

<target name="HeelpAdsImport_patrick_email" xsi:type="Mail"
        smtpServer="XXXXX"
        smtpPort="25"
        smtpAuthentication="Basic"
        smtpUserName="YYYYYY"
        smtpPassword="*ZZZZZZ"
        enableSsl="false"
        from="DDDDDDDDDD"
        to="EEEEEEEEEEE"
        layout="${longdate} ${uppercase:${level}} ${callsite:className=true:includeSourcePath=true:methodName=true} ${message}"
      />

您可以为您的电子邮件目标使用批处理将多个日志条目批处理到一封电子邮件中。它支持在指定的时间跨度(以毫秒为单位设置
flushTimeout
)和/或指定数量的日志条目(将
bufferSize
设置为条目数量)内进行批处理

编辑:将当前目标包装在
中,如下所示:

<target xsi:type="BufferingWrapper"
          name="MailBuffer"
          slidingTimeout="false"
          bufferSize="100"
          flushTimeout="-1">
    <target name="HeelpAdsImport_patrick_email" xsi:type="Mail"
            smtpServer="XXXXX"
            smtpPort="25"
            smtpAuthentication="Basic"
            smtpUserName="YYYYYY"
            smtpPassword="*ZZZZZZ"
            enableSsl="false"
            from="DDDDDDDDDD"
            to="EEEEEEEEEEE"
            layout="${longdate} ${uppercase:${level}} ${callsite:className=true:includeSourcePath=true:methodName=true} ${message}${newline}"
          />
</target>

编辑2:您在退出程序之前是否会打电话


编辑3:版面渲染器应在电子邮件中生成换行符(在上面的
版面属性的末尾)。

。。。电子邮件有什么问题吗?您好,谢谢,我每次调用logger.Log(LogLevel.Error…)时都会收到一封电子邮件,我在过程中多次调用它,但我只想在过程结束时收到一封电子邮件。您好,谢谢,但是我在哪里设置“BufferingWrapper”和“wrappedTargetType”在我的代码中,先缓冲然后在结尾发送?您好,似乎当未达到bufferSize中设置的条目数时,结尾处的电子邮件不会发送。我已在布局属性的结尾添加了一个换行布局渲染器,该渲染器将在您的电子邮件中生成一个新行。是的,您不能在发布后的前24小时内奖励这只是一个小小的提醒:确保将您的目标更改为包装器的名称,而不是原始邮件目标;)即使它是嵌套的,它仍将直接接受它,而不是缓冲区:)
logger.Log(LogLevel.Info, " ----- New Ad Success! - auto.id: " + auto.id + " | auto.plate: " + auto.plate);

logger.Log(LogLevel.Error, "| continue error #4 - auto.id: " + auto.id);
<target xsi:type="BufferingWrapper"
          name="MailBuffer"
          slidingTimeout="false"
          bufferSize="100"
          flushTimeout="-1">
    <target name="HeelpAdsImport_patrick_email" xsi:type="Mail"
            smtpServer="XXXXX"
            smtpPort="25"
            smtpAuthentication="Basic"
            smtpUserName="YYYYYY"
            smtpPassword="*ZZZZZZ"
            enableSsl="false"
            from="DDDDDDDDDD"
            to="EEEEEEEEEEE"
            layout="${longdate} ${uppercase:${level}} ${callsite:className=true:includeSourcePath=true:methodName=true} ${message}${newline}"
          />
</target>