Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog - how to reuse layout of type 'JsonLayout' in multiple targets

Tags:

nlog

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 ?

like image 558
user3359453 Avatar asked Sep 15 '25 16:09

user3359453


2 Answers

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>
like image 139
Ryan Gaudion Avatar answered Sep 17 '25 19:09

Ryan Gaudion


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

like image 40
Julian Avatar answered Sep 17 '25 20:09

Julian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!