Since the release of NLog 4.0.0, you can use a layout that displays events as structured JSON documents.
Example from the NLog project site :
<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>
Edit: You can also create it in code.
Here is a link to a specific part of the documentation.
And here is a copied example:
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=}"), } },
Read the docs for more details.
source share