1

I have a service running hourly that uses NLog to send mail when exception occurs. So how do I configure it to not log at Saturday and Sunday? Can I even do that?

Here's my target:

<target name="m"
   xsi:type="Mail"
   html="true"
   smtpServer="*******"
   smtpPort="*******"
   smtpAuthentication="*******"
   smtpUserName="*******"
   smtpPassword="*******"
   enableSsl="true"
   subject="Exception"
   from="*******"
   to="*******"
   layout ="${longdate} ${uppercase:${level}}${newline} ${callsite:className=true:includeSourcePath=true:methodName=true}${newline} ${message}${newline}"
   useSystemNetMailSettings="false" />

And rule:

<logger name="*" minlevel="Fatal" writeTo="m" />
Ivan Maslov
  • 168
  • 2
  • 13
  • You want the service to run, but not log anything? Why? –  Sep 15 '17 at 14:22
  • I want normal logs but no email (I have 2 targets). Also I set it to send to 5 different emails. This means more than 300 emails send if it crashes every time – Ivan Maslov Sep 15 '17 at 14:41
  • It seems to me that the proper course of action is fixing the application so it doesn't crash. –  Sep 15 '17 at 15:13
  • @Amy Ok, here's how it goes: my app gets data from 3rd party API. Sometimes these APIs returm Exception. So my app doesn't crash. I just want to know if 3rd party API crashes. But not on my weekend. – Ivan Maslov Sep 18 '17 at 07:38

1 Answers1

1

Maybe use the PostFilteringWrapper, and use a filter condition like:

  • not equals('${date:format=ddd}', 'Sat')
  • not equals('${date:format=ddd}', 'Sun')

https://github.com/NLog/NLog/wiki/PostFilteringWrapper-target

https://github.com/NLog/NLog/wiki/When-filter

https://learn.microsoft.com/en-us/dotnet/standard/base-types/how-to-extract-the-day-of-the-week-from-a-specific-date

CFinck
  • 3,171
  • 1
  • 16
  • 10
Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70