Skip to content
Rolf Kristensen edited this page May 9, 2023 · 18 revisions

A specialized layout that renders to XML.

Platforms Supported: All

Introduced in NLog 4.6

<target name="xmlFile" xsi:type="File" fileName="${logFileNamePrefix}.xml" >
      <layout xsi:type="XmlLayout" includeEventProperties="false" elementName='logevent'>
              <attribute name="time" layout="${longdate}" />
              <attribute name="level" layout="${level:upperCase=true}"/>
              <element name="message" value="${message}" />
      </layout>
</target>

This would write:

<logevent time="2010-01-01 12:34:56.0000" level="ERROR">
   <message>hello, world</message>
</logevent>

Parameters

Root

  • ElementName - Name of the top level XML element Default: logevent

  • ElementValue - Value inside the top level XML element Layout

  • ElementEncode - Xml Encode the value for the top level XML element Boolean Default: True

    Ensures always valid XML, but gives a performance hit

  • IndentXml - Insert newlines and simplistic indention of output. Boolean. Default: False

  • IncludeEmptyValue - Whether a ElementValue with empty value should be included in the output Boolean Default: False

  • MaxRecursionLimit - How far should the XML serializer follow object references before backing off. Integer. Default 1 (0 = No object reflection)

  • IncludeEventProperties - Include all events properties of a logevent? Default: false.

    Before NLog 5.0 option was named IncludeAllProperties

  • IncludeScopeProperties - Indicates whether to include ScopeContext Properties dictionary. Default: false.

    Before NLog 5.0 option was named IncludeMdlc or IncludeMdc

  • ExcludeProperties - Comma separated string with names which properties to exclude. Only used when IncludeEventProperties is true. Case sensitive. Default empty When a name contains a comma, single quote the value. E.g. 'value,withquote',value2.

  • PropertiesElementName - Element name to use when rendering properties. Support string-format where {0} means property-key-name. Skips closing element tag when having configured PropertiesElementValueAttribute

  • PropertiesElementKeyAttribute - Attribute name to use when rendering property-key. When null (or empty) then key-attribute is not included. Will replace newlines in attribute-value with &#13;&#10;

  • PropertiesElementValueAttribute - Attribute name to use when rendering property-value. When null (or empty) then value-attribute is not included and value is formatted as XML-element-value. Skips closing element tag when using attribute for value Will replace newlines in attribute-value with &#13;&#10;

  • PropertiesCollectionItemName - Element name to use for rendering IList-collections items

Elements

<element> defines childs elements to render:

  • Name - Name of the element. Default 'item'
  • Value - Value inside the element Layout
  • Encode - Xml Encode the value Boolean Default: True

Configuration parameters for Root can also be used for each <element>-element.

Attributes

<attribute> defines the attributes to render:

  • Name - Name of the attribute. Required.
  • Layout - Layout that will be rendered as the attribute's value. Layout Required.
  • Encode - Xml Encode the attribute value. Boolean Default: True
  • IncludeEmptyValue - - Whether an attribute with empty value should be included in the output Boolean

Examples

XML with attributes and elements

XmlLayout

<layout type='XmlLayout' >
     <attribute name='logger' layout='${logger}' includeEmptyValue='true' />
     <attribute name='level' layout='${uppercase:${level}}' includeEmptyValue='true' />
     <element name='message' value='${message}' />
     <element name='exception'>
         <attribute name='tyoe' layout='${exception:format=type}' />
         <attribute name='message' layout='${exception:format=message}' />
         <attribute name='stacktrace' layout='${exception:format=tostring}' />
     </element>
</layout>

Xml Output

<logevent logger="logger1" level="INFO">
  <message>My log message</message>
  <exception>My exception message</exception>
  <location class-name="MyNamespace.MyClass"></location>
</logevent>

LogEventInfo Properties Default Output

XmlLayout

<layout xsi:type="XmlLayout" includeEventProperties="true">
</layout>

Xml Output

<logevent>
  <property key="PropertyName">PropertyValue</property>
</logevent>

LogEventInfo Properties with element-name override

XmlLayout

<layout xsi:type="XmlLayout" includeEventProperties="true" propertiesElementName="myparm">
</layout>

Xml Output

<logevent>
  <myparm key="PropertyName">PropertyValue</myparm>
</logevent>

LogEventInfo Properties with element-name as property-name

XmlLayout

<layout xsi:type="XmlLayout" includeEventProperties="true" propertiesElementKeyAttribute="" propertiesElementName="{0}">
</layout>

Xml Output

<logevent>
  <PropertyName>PropertyValue</PropertyName>
</logevent>

LogEventInfo Properties Value as attribute:

XmlLayout

<layout xsi:type="XmlLayout" includeEventProperties="true" propertiesElementValueAttribute="value">
</layout>

Xml Output

<logevent>
  <property key="PropertyName" value="PropertyValue"/>
</logevent>

LogEventInfo Properties Dictionary-object Default Output

XmlLayout

<layout xsi:type="XmlLayout" includeEventProperties="true">
</layout>

Xml Output

<logevent>
  <property key="PropertyName"><property key="DictionaryKey">DictionaryValue</property></property>
</logevent>

LogEventInfo Properties List-object Default Output

XmlLayout

<layout xsi:type="XmlLayout" includeEventProperties="true">
</layout>

Xml Output

<logevent>
  <property key="PropertyName"><item>ListValue1</item><item>ListValue2</item></property>
</logevent>

LogEventInfo Properties List-object with element-name override

XmlLayout

<layout xsi:type="XmlLayout" includeEventProperties="true" propertiesCollectionItemName="element">
</layout>

Xml Output

<logevent>
  <property key="PropertyName"><element>ListValue1</element><element>ListValue2</element></property>
</logevent>
Clone this wiki locally