简体   繁体   中英

How to serialize property to json in nlog

I have following configuration:

<target>
<layout xsi:type="JsonLayout">
 <attribute name="Foo" layout="${event-properties:item=Foo:@}" />
</layout>
</target>

This is the resulting doc pushed to output:

{
   "Foo": "{ \"Nested\": \"Json\" } "
}

Instead I would like following result:

{
   "Foo": {  "Nested": "Json" }
}

However I want to render just Foo property as json, not all event-properties. Nested json layout does not have source. How to get around that?

NLog JsonLayout will automatically fix the output coming from attribute-Layout, so reserved characters becomes escaped correctly.

But if you already know the output will be valid json (happens when using format=@ ), then you can specify encode="false" like this:

<target>
 <layout xsi:type="JsonLayout">
   <attribute name="Foo" layout="${event-properties:item=Foo:format=@}" encode="false" />
 </layout>
</target>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM