Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog - Write to another target if message starts with certain words

Tags:

c#

nlog

I would like to know if it is possible to write the logs to another target if the message that we want to write begins with certain words, a prefix.

I can't do it at the class level right now, that's why I'm checking if it's possible to get what I want in an easier way than modifying the code

like image 306
Moshe HAYUN Avatar asked Aug 31 '25 04:08

Moshe HAYUN


1 Answers

Yes, this is possible.

It could be configured like this:

<logger name="*" writeTo="target-only-if-message-has-prefix">
  <filters defaultAction='Ignore'>
    <when condition="starts-with('${message}', 'MyPrefix')" action="Log" />
  </filters>
</logger> 

See

  • https://github.com/NLog/NLog/wiki/Filtering-log-messages
  • https://github.com/NLog/NLog/wiki/When-filter
like image 132
Julian Avatar answered Sep 02 '25 18:09

Julian