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>
Example output:
{ "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" />
true
Introduced in NLog 4.1
true
Introduced in NLog 4.4.7
false
Introduced in NLog 4.5
false
,
Introduced in NLog 4.6.8. Before NLog 5.0 the default was true. After NLog 6.0 it has no effect.
true
.
Introduced in NLog 4.1. Before NLog 6.0 the default was
false
false
.
Introduced in NLog v5.1.4
{}
. Default = true
.
Introduced in NLog 4.3.7
false
.
Introduced in NLog 4.4.10
false
.
Before NLog 5.0 option was named IncludeAllProperties
false
.
Before NLog 5.0 option was named IncludeMdlc or IncludeMdc
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
Introduced in NLog 4.7.7
true
.
Introduced in NLog 4.4.7
1
(0 = No object reflection)
Introduced in NLog 4.5. Before NLog 5.0 the default was
0
(No object reflection)
/
will be converted to \/
. Default = false
.
Introduced in NLog 4.6.8. Before NLog 5.0 the default was
true
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 } };
<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!" } }
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4