loggingnlognlog-configuration

NLog config not reading boolean Property


I have the following NLog filter (logger outputs to the DB):

<logger name="*" minlevel="Error" writeTo="Database" >
  <filters>
    <when condition="${event-properties:item=LogToDatabase} == false" action="Ignore"/>
  </filters>
</logger>

And that is how I call NLog:

Log.Error().Message("test").Property("LogToDatabase", false).Write();

The config seems to be not working with the bool values, however the only way I have managed to make it work is using strings in the config like this:

<when condition="'${event-properties:item=LogToDatabase}' == 'False'" action="Ignore"/>

Then calling NLog with a string Property:

Log.Error().Message("test").Property("LogToDatabase", false.ToString()).Write();

Is there a way to have a boolean check in the config?


Solution

  • Layout Renderers in NLog only renders as text. You can see code for event-properties here: https://github.com/NLog/NLog/blob/e0650c42b4ab3660abc60717e50535d20763289c/src/NLog/LayoutRenderers/EventPropertiesLayoutRenderer.cs

    So you can not check NLog expressions as bool.