0

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?

Kal
  • 141
  • 1
  • 12

1 Answers1

1

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.

Alexey.Petriashev
  • 1,634
  • 1
  • 15
  • 19
  • Can format be used to somehow set the type to Boolean like the DateTime is shown in the example? – Kal May 22 '18 at 19:29
  • Format can be used for additional formatting. But the result is text. – Alexey.Petriashev May 22 '18 at 19:31
  • You also can write custom condition method that evaluates to bool. Example: https://github.com/NLog/NLog/search?utf8=%E2%9C%93&q=myrandom&type= Standard condition methods: https://github.com/NLog/NLog/blob/19de4feb0a6f1292e9d0c69da5c75b73efda3ea1/src/NLog/Conditions/ConditionMethods.cs – Alexey.Petriashev May 22 '18 at 19:52
  • Custom condition looks a lot cleaner. – Kal May 22 '18 at 20:22
  • just a short question... if bools do not translate properly in NLog expressions, what do they translate into? Do they stay as objects (.Property(object, object))? – Kal May 23 '18 at 04:32
  • They stay objects but renders as string with some format and culture – Alexey.Petriashev May 23 '18 at 05:47