TW_KuanLun
TW_KuanLun

Reputation: 87

Is it possible that NLog send email when error occurs and includes the previous five logs with different levels?

I read the documents of BufferingWrapper, I don't know how to do it...

I made a simple example.

Suppose I logged some information I needed before the error occurred.

logger.LogInformation("a");
logger.LogInformation("b");
logger.LogInformation("c");
logger.LogError(new Exception("My Custom Exception"), "Test Mail1");
logger.LogInformation("1");
logger.LogWarning("2");
logger.LogWarning("3");
logger.LogInformation("4");
logger.LogInformation("5");
logger.LogError(new Exception("My Custom Exception"), "Test Mail2");

I expected to receive two emails like these

2020-05-14 XX: XX: XX.XXXX [1] INFO ConsoleApp1.Runner a
2020-05-14 XX: XX: XX.XXXX [1] INFO ConsoleApp1.Runner b
2020-05-14 XX: XX: XX.XXXX [1] INFO ConsoleApp1.Runner c
2020-05-14 XX: XX: XX.XXXX [1] ERROR ConsoleApp1.Runner Test Mail1 System.Exception: My Custom Exception
2020-05-14 XX: XX: XX.XXXX [1] INFO ConsoleApp1.Runner 1
2020-05-14 XX: XX: XX.XXXX [1] WARN ConsoleApp1.Runner 2
2020-05-14 XX: XX: XX.XXXX [1] WARN ConsoleApp1.Runner 3
2020-05-14 XX: XX: XX.XXXX [1] INFO ConsoleApp1.Runner 4
2020-05-14 XX: XX: XX.XXXX [1] INFO ConsoleApp1.Runner 5
2020-05-14 XX: XX: XX.XXXX [1] ERROR ConsoleApp1.Runner Test Mail2 System.Exception: My Custom Exception

But I can only got two emails error message.

2020-05-14 XX: XX: XX.XXXX [1] ERROR ConsoleApp1.Runner Test Mail1 System.Exception: My Custom Exception
2020-05-14 XX: XX: XX.XXXX [1] ERROR ConsoleApp1.Runner Test Mail2 System.Exception: My Custom Exception

How can I include the previous five logs with different levels when the error occurs?

This is y nlog.config

<?xml version="1.0" encoding="utf-8" ?>
<!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogFile="c:\temp\console-example-internal.log"
      internalLogLevel="Info" >

  <extensions>
    <add assembly="NLog.MailKit"/>
  </extensions>
  <targets>
    <target xsi:type="Mail"
          name="MailTarget"
          layout="${longdate:universalTime=true} [${threadid}] ${uppercase:${level}} ${logger} ${message} ${exception:format=tostring}"
          addNewLines="true"
          subject="[My Error Log - Dev] Execption"
          to=""
          from=""
          smtpServer="" />
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <logger name="*" level="Error" writeTo="MailTarget" />
  </rules>
</nlog>

Upvotes: 2

Views: 1532

Answers (1)

Rolf Kristensen
Rolf Kristensen

Reputation: 19867

Well a good starting point is activating the BufferingWrapper, like this:

 <targets>
    <target xsi:type"BufferingWrapper" name="MailBuffer">
       <target xsi:type="Mail" name="MailTarget" />
    </target>
 </targets>
 <rules>
    <logger name="*" level="Error" writeTo="MailBuffer" />
 </rules>

But now you have just enabled buffered writing of Error-LogEvents to the MailTarget, and missing the features:

  • Should also include Info-LogEvents and not just Error-LogEvents.

    • This can be implemented by changing logging rules from level="Error" to minLevel="Info".
  • Should only write the Info-LogEvents when an Error-LogEvent occurs.

    • This can be implemented by using AutoFlushWrapper, so it triggers flush of the buffered LogEvents on Error-LogEvent
    • Combined with using overflowAction="Discard" on the BufferingWrapper

Example:

<targets>
  <target xsi:type="AutoFlushWrapper" name="MailErrorFlush"
    condition="level >= LogLevel.Error"
    flushOnConditionOnly="true">
     <target xsi:type="BufferingWrapper" name="MailBuffer"
       bufferSize="50"
       overflowAction="Discard">
        <target xsi:type="Mail" name="MailTarget" />
     </target>
  </target>
</targets>
<rules>
  <logger name="*" minlevel="Info" writeTo="MailErrorFlush" />
</rules>

See also: https://github.com/nlog/NLog/wiki/BufferingWrapper-target#send-batch-when-triggered-by-event

Upvotes: 3

Related Questions