28

I want to send only one email with all the errors I get from my C# Console Application.

I have the Targets:

<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}"
      />

I have an Info rule and an Error rule:

<logger name="*" minlevel="Info" writeTo="HeelpAdsImport_log" />

<logger name="*" minlevel="Error" writeTo="HeelpAdsImport_patrick_email" />

I have several calls in the code for each other:

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);
Patrick
  • 2,995
  • 14
  • 64
  • 125
  • So... what's the problem with the email? – eddie_cat Mar 27 '14 at 13:11
  • Hi thanks, I receive an email everytime I call logger.Log(LogLevel.Error... and I call it several times in the process, but I only want to receive one email at the end of the process – Patrick Mar 27 '14 at 17:45

1 Answers1

42

You can use a BufferingWrapper for your email target to batch multiple log entries into one email. It supports batching for a specified span of time (set flushTimeout in milliseconds) and/or for a specified number of log entries (set bufferSize to the number of entries).

Edit: Wrap your current target inside a <target type="BufferingWrapper"> like so:

<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>

Edit 2: Do you call LogManager.Flush() before exiting your program?

Edit 3: The ${newline} layout renderer should produce a line break in your email (at the end of the layout attribute above).

andyp
  • 6,229
  • 3
  • 38
  • 55
  • Hi thanks, but where do I set the "BufferingWrapper" and "wrappedTargetType" in my code to Buffer first and send in the end? – Patrick Mar 31 '14 at 15:04
  • Hi, it seems that when the number of entries set in bufferSize is not reached, the email at the end is not sended – Patrick Mar 31 '14 at 18:31
  • Great! it's working fine now! Just one last question, how can I break the line in the email? it's not make a new line, only in the file – Patrick Mar 31 '14 at 18:59
  • 1
    I've added a newline layout renderer at the end of the layout attribute - that should produce a new line in your email. – andyp Mar 31 '14 at 19:05
  • Perfect! Thank you very much! ;) I get "You may award your bounty in 18 hours." :( – Patrick Mar 31 '14 at 19:11
  • 1
    Yes, you can't award a bounty within the first 24 hours after posting it. – andyp Apr 01 '14 at 14:07
  • 4
    Just a small reminder: be sure to change your target to the name of the wrapper, not your original mail target ;) even though it's nested, it will still accept it directly and not buffer :) – NKCSS Sep 07 '16 at 08:03