jsonasp.net-corenlogstructured-logging

NLog Database Parameter + .NET Core: all-event-properties in JSON?


I am using the Database target and trying to figure out a way to send all-event-properties as a parameter in JSON format; I see that parameter includes a property for "format," but when I do format="json" it doesn't produce the desired effect.

Here's what I have in my nlog.config file:

<target name="database">
    <parameter name="@properties" layout="${all-event-properties}" format="json" />
</target>

Unfortunately my properties are still getting logged in the format

property1=property1, property2=property2, property3=property3

What I'm expecting to see is

{
    "property1": "property1",
    "property2": "property2",
    "property3": "property3"
}

Please advise. Thank you!


Solution

  • You can do this:

    <target name="database" type="database">
        <parameter name="@properties">
            <layout type="JsonLayout" includeAllProperties="true" maxRecursionLimit="1" escapeForwardSlash="false" />
        </parameter>
    </target>
    

    See also: https://github.com/nlog/nlog/wiki/JsonLayout