Table of Contents

Class XmlLayout

Namespace
log4net.Layout
Assembly
log4net.dll

Layout that formats the log events as XML elements.

public class XmlLayout : XmlLayoutBase, ILayout, IOptionHandler
Inheritance
XmlLayout
Implements
Inherited Members

Remarks

The output of the XmlLayout consists of a series of log4net:event elements. It does not output a complete well-formed XML file. The output is designed to be included as an external entity in a separate file to form a correct XML file.

For example, if abc is the name of the file where the XmlLayout output goes, then a well-formed XML file would be:

<?xml version="1.0" ?>

<!DOCTYPE log4net:events SYSTEM "log4net-events.dtd" [<!ENTITY data SYSTEM "abc">]>

<log4net:events version="1.2" xmlns:log4net="http://logging.apache.org/log4net/schemas/log4net-events-1.2>
    &data;
</log4net:events>

This approach enforces the independence of the XmlLayout and the appender where it is embedded.

The version attribute helps components to correctly interpret output generated by XmlLayout. The value of this attribute should be "1.2" for release 1.2 and later.

Alternatively the Header and Footer properties can be configured to output the correct XML header, open tag and close tag. When setting the Header and Footer properties it is essential that the underlying data store not be appendable otherwise the data will become invalid XML.

Constructors

XmlLayout()

Constructs an XmlLayout

public XmlLayout()

XmlLayout(bool)

Constructs an XmlLayout.

public XmlLayout(bool locationInfo)

Parameters

locationInfo bool

Remarks

The LocationInfo option takes a boolean value. By default, it is set to false which means there will be no location information output by this layout. If the the option is set to true, then the file name and line number of the statement at the origin of the log statement will be output.

If you are embedding this layout within an SmtpAppender then make sure to set the LocationInfo option of that appender as well.

Properties

Base64EncodeMessage

Set whether or not to base64 encode the message.

public bool Base64EncodeMessage { get; set; }

Property Value

bool

Remarks

By default the log message will be written as text to the xml output. This can cause problems when the message contains binary data. By setting this to true the contents of the message will be base64 encoded. If this is set then invalid character replacement (see InvalidCharReplacement) will not be performed on the log message.

Base64EncodeProperties

Set whether or not to base64 encode the property values.

public bool Base64EncodeProperties { get; set; }

Property Value

bool

Remarks

By default the properties will be written as text to the xml output. This can cause problems when one or more properties contain binary data. By setting this to true the values of the properties will be base64 encoded. If this is set then invalid character replacement (see InvalidCharReplacement) will not be performed on the property values.

Prefix

The prefix to use for all element names

public string Prefix { get; set; }

Property Value

string

Remarks

The default prefix is log4net. Set this property to change the prefix. If the prefix is set to an empty string then no prefix will be written.

Methods

ActivateOptions()

Initialize layout options

public override void ActivateOptions()

Remarks

This is part of the IOptionHandler delayed object activation scheme. The ActivateOptions() method must be called on this object after the configuration properties have been set. Until ActivateOptions() is called this object is in an undefined state and must not be used.

If any of the configuration properties are modified then ActivateOptions() must be called again.

Builds a cache of the element names

FormatXml(XmlWriter, LoggingEvent)

Does the actual writing of the XML.

protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)

Parameters

writer XmlWriter

The writer to use to output the event to.

loggingEvent LoggingEvent

The event to write.

Remarks

Override the base class FormatXml(XmlWriter, LoggingEvent) method to write the LoggingEvent to the System.Xml.XmlWriter.