I'm current using NLog to log into Json format. I want to log event args in my logs under "eventProperties" without them being included in the message attribute.
Current nlog.config:
<target name="jsonFile" xsi:type="Console">
<layout xsi:type="JsonLayout">
<attribute name="time" layout="${longdate}" />
<attribute name="level" layout="${level:upperCase=true}"/>
<attribute name="message" layout="${message}" />
<attribute name="eventProperties" encode="false" >
<layout xsi:type='JsonLayout' includeAllProperties="true" maxRecursionLimit="6"/>
</attribute>
</layout>
</target>
Currently call it with:
_logger.LogDebug(20, "Doing hard work! {logInfo}", t2);
Which logs the following:
{ "time": "2022-05-19 08:24:15.1395", "level": "DEBUG", "message": "Doing hard work! Test", "eventProperties": { "logInfo": {"MyProperty":"Hello2", "MyProperty2":"Testing2", "MyModel":{"MyProperty":"Hello", "MyProperty2":"Testing"}}, "EventId": 20 } }
However, I want to be able to log with the below without it stopping t2 from being logged in the eventProperties section of the JSON:
_logger.LogDebug(20, "Doing hard work!", t2);
and output for the message property to not include test
in it anymore:
"message": "Doing hard work!"
How can I go about removing the property from the message without it being removed from eventProperties?
EDIT - I'm using the Microsoft ILogger Abstraction for .LogDebug()
You can output the message-template (instead of the formatted message) using raw=true
:
<attribute name="message" layout="${message:raw=true}" />
See also: https://github.com/NLog/NLog/wiki/How-to-use-structured-logging