In NLog- for layout of json I am using this -:
<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>
I have multiple targets in my project that uses same layout behavior, How can I reuse this layout ?
As of NLog 5 this is available here - https://github.com/NLog/NLog/wiki/Configuration-file#variables
With the following example:
<nlog>
<variable name="myJsonLayout">
<layout type="JsonLayout">
<attribute name="message" value="${messsage}">
</layout>
</variable>
<targets>
<target name="debug" type="Debug" layout="${myJsonLayout}" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="debug" />
</rules>
</nlog>
You can reuse it when configuring NLog with the API (eg from C#)
When using the xml config currently you need to copy paste. The variables in the xml can't contain layouts like these.
Update: this will be supported in the upcoming NLog 5 release, see https://github.com/NLog/NLog/pull/3459
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With