Skip to content
John Korsnes edited this page Aug 4, 2016 · 13 revisions

A specialized layout that renders to JSON.

Added in NLog 4.0

<target name="jsonFile" xsi:type="File" fileName="${logFileNamePrefix}.json">
      <layout xsi:type="JsonLayout">
              <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.

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

##Parameters

  • name: required. The name of the key in JSON
  • layout: The layout for they key.
  • encode: Enable or disable JSON encoding for the attribute. Enabled by default. (Added in NLog 4.1)
  • suppressSpaces: Enable to suppress extra spaces in the output JSON. Disabled by default. (Added in NLog 4.1)
  • renderEmptyObject: Gets or sets the option to render the empty object value {}, default true. (Added in NLog 4.3.7)

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

Nested JSON layouts:

From the API:

var jsonLayout = new JsonLayout
{
    Attributes =
    {
        new JsonAttribute("type", "${exception:format=Type}"),
        new JsonAttribute("message", "${exception:format=Message}"),
        new JsonAttribute("innerException", new JsonLayout
        {

            Attributes =
            {
                new JsonAttribute("type", "${exception:format=:innerFormat=Type:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}"),
                new JsonAttribute("message", "${exception:format=:innerFormat=Message:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}"),
            }
        },
        //don't escape layout
        false)
    }
};

returns: { "type": "NLog.NLogRuntimeException", "message": "test", "innerException": { "type": "System.NullReferenceException", "message": "null is bad!" } }

From XML

<nlog>
  <targets>
    <target name='jsonFile' type='File' fileName='log.json'>
      <layout type='JsonLayout'>
        <attribute name='time' layout='${longdate}' />
        <attribute name='level' layout='${level:upperCase=true}'/>
        <attribute name='nested' encode='false'  >
          <layout type='JsonLayout'>
            <attribute name='message' layout='${message}' />
            <attribute name='exception' layout='${exception}' />
          </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", "nested": { "message": "this is message", "exception": "test" } }

RenderEmptyObject

var jsonLayout = new JsonLayout
{
    Attributes =
    {
        new JsonAttribute("type", "${exception:format=Type}"),
        new JsonAttribute("message", "${exception:format=Message}"),
        new JsonAttribute("innerException", new JsonLayout
        {

            Attributes =
            {
                new JsonAttribute("type", "${exception:format=:innerFormat=Type:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}"),
                new JsonAttribute("message", "${exception:format=:innerFormat=Message:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}"),
            },
            RenderEmptyObject = false
        },
        //don't escape layout
        false)
    }
   
};

Writing without an exception will render { "type": "NLog.NLogRuntimeException", "message": "test" }

with RenderEmptyObject=true (default) it will render:

"type": "NLog.NLogRuntimeException", "message": "test", "innerException": { } }

Clone this wiki locally