Skip to content
Artem Grunin edited this page Jul 13, 2023 · 59 revisions

A specialized layout that renders to JSON.

Platforms Supported: All

Introduced with NLog 4.0

<target name="jsonFile" xsi:type="File" fileName="log-file.json" >
      <layout xsi:type="JsonLayout" includeEventProperties="Boolean" excludeProperties="Comma-separated list (string)">
              <attribute name="time" layout="${longdate}" />
              <attribute name="level" layout="${level:upperCase=true}"/>
              <attribute name="message" layout="${message}" />
       </layout>
</target>

This would write:

{ "time": "2010-01-01 12:34:56.0000", "level": "ERROR", "message": "hello, world" }

Added in NLog 4.1

Optional encode parameter.

You can disable JSON encoding by setting encode="false". This will let you to write any string without JSON encoding. Including custom JSON (Ex. boolean/numeric values)

<attribute name="Details" layout="${event-properties:item=Details}" encode="false" />

Parameters

  • Attribute
    • name - Required. The name of the JSON-key
    • layout - The layout for the JSON-value (Can be a nested JsonLayout)
    • encode - Enable or disable JSON encoding for the attribute. Default = true

      Introduced in NLog 4.1

    • escapeUnicode - Escape unicode-characters (non-ascii) using \u. Default = true

      Introduced in NLog 4.4.7

    • IncludeEmptyValue - Include attribute when Layout output is empty. Default = false

      Introduced in NLog 4.5

    • EscapeForwardSlash - Should forward slashes also be escaped. Default = false,

      Introduced in NLog 4.6.8. Before NLog 4.7 it did not inherit value from parent. Before NLog 5.0 the default was true

  • suppressSpaces - Enable to suppress extra spaces in the output JSON. Default = false.

    Introduced in NLog 4.1

  • indentJson - Enables indent for JSON pretty format with newlines. Default = false.

    Introduced in NLog v5.1.4

  • renderEmptyObject - When no json-attributes, then it should still render empty object-value {}. Default = true.

    Introduced in NLog 4.3.7

  • IncludeGdc - Indicates whether to include contents of the GlobalDiagnosticsContext (GDC) dictionary. Default = false.

    Introduced in NLog 4.4.10

  • IncludeEventProperties - Include all events properties of a logevent? Default = false.

    Before NLog 5.0 option was named IncludeAllProperties

  • IncludeScopeProperties - Indicates whether to include ScopeContext Properties dictionary. Default = false.

    Before NLog 5.0 option was named IncludeMdlc or IncludeMdc

  • excludeProperties - comma separated string with names which properties to exclude. Only used when includeEventProperties is true. Case insensitive. Default empty When a name contains a comma, single quote the value. E.g. 'value,withquote',value2.

    Introduced in NLog 4.4

  • excludeEmptyProperties - Exclude event properties with value null or empty (Also checks GDC, MDC, MDLC).

    Introduced in NLog 4.7.7

  • EscapeUnicode - escape non-ascii characters? Boolean. Default true.

    Introduced in NLog 4.4.7

  • MaxRecursionLimit - How far should the JSON serializer follow object references before backing off. Integer. Default 1 (0 = No object reflection)

    Introduced in NLog 4.5. Before NLog 5.0 the default was 0(No object reflection)

  • EscapeForwardSlash - Should forward slashes be escaped? If true, / will be converted to \/. Default = false.

    Introduced in NLog 4.6.8. Before NLog 5.0 the default was true

Notes

  • Currently the layout will always create a non-nested object with properties.
  • The JSON will be written on one line, so no newlines.

Advanced examples

From the API:

var jsonLayout = new JsonLayout
{
    Attributes =
    {
        new JsonAttribute("time", "${longdate}"),
        new JsonAttribute("level", "${level}"),
        new JsonAttribute("message", "${message}"),
        new JsonAttribute("properties", new JsonLayout { IncludeEventProperties = true, maxRecursionLimit = 2 }, encode: false),
        new JsonAttribute("exception", new JsonLayout
        {
            Attributes =
            {
                new JsonAttribute("type", "${exception:format=type}"),
                new JsonAttribute("message", "${exception:format=message}"),
                new JsonAttribute("stacktrace", "${exception:format=tostring}"),
            }
        },
        encode: false) // don't escape layout
    }
};

From XML

<nlog>
  <targets>
    <target xsi:type="File" name="jsonFile" fileName="c:\temp\nlog-json-nested-${shortdate}.log">
      <layout xsi:type="JsonLayout">
         <attribute name="time" layout="${longdate}" />
         <attribute name="level" layout="${level}" />
         <attribute name="message" layout="${message}" />
         <attribute name="properties" encode="false" >
             <layout xsi:type="JsonLayout" includeEventProperties="true" maxRecursionLimit="2" />
         </attribute>
         <attribute name="exception" encode="false">
             <layout xsi:type="JsonLayout">
                 <attribute name="type" layout="${exception:format=type}" />
                 <attribute name="message" layout="${exception:format=message}" />
                 <attribute name="stacktrace" layout="${exception:format=tostring}" />
             </layout>
         </attribute>
      </layout>
    </target>
  </targets>
  <rules>
      <logger name="*" minlevel="Debug" writeTo="jsonFile" />
  </rules>
</nlog>

will render: { "time": "2016-10-30 13:30:55.0000", "level": "Info", "message": "this is message", "properties": { "myProperty": "myValue" }, "exception": { "type": "System.NullReferenceException", "message": "null is bad!" } }

pretty printed:

{
  "time": "2016-10-30 13:30:55.0000",
  "level": "Info",
  "message": "this is message",
  "properties": {
    "myProperty": "myValue"
  },
  "exception": {
    "type": "System.NullReferenceException",
    "message": "null is bad!"
  }
}
Clone this wiki locally