Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog filter based on time of the day

Tags:

nlog

I am trying to use NLog conditions to filter messages that are not generated at certain time

the following is my sample

<logger name="*" minlevel="Info" writeTo="InfoMail" >
  <when condition="starts-with('${date:format=hh}', '01')" action="Ignore"/>
</logger>

However, Nlog seemed not able to pick up '${date:format=hh}' and I still receive email even the messages are generated at one o'clock. Is there any way to get around this?

like image 470
Wei Ma Avatar asked Jan 21 '26 04:01

Wei Ma


1 Answers

The when conditions should be put inside a filters element as show in Conditions - Examples and specified in the NLog.xsd (the syntax on the When filter page is wrong)

So the correct usage is:

<logger name="*" minlevel="Info" writeTo="Console" >
  <filters>
    <when condition="starts-with('${date:format=hh}', '01')" action="Ignore"/>
  </filters>
</logger>
like image 171
nemesv Avatar answered Jan 23 '26 21:01

nemesv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!