Table of Contents

Class XmlConfigurator

Namespace
log4net.Config
Assembly
log4net.dll

Use this class to initialize the log4net environment using an Xml tree.

public sealed class XmlConfigurator
Inheritance
XmlConfigurator
Inherited Members

Remarks

Configures a ILoggerRepository using an Xml tree.

Methods

Configure()

Automatically configures the log4net system based on the application's configuration settings.

public static ICollection Configure()

Returns

ICollection

Remarks

Each application has a configuration file. This has the same name as the application with '.config' appended. This file is XML and calling this function prompts the configurator to look in that file for a section called log4net that contains the configuration data.

To use this method to configure log4net you must specify the Log4NetConfigurationSectionHandler section handler for the log4net configuration section. See the Log4NetConfigurationSectionHandler for an example.

See Also

Configure(FileInfo)

Configures log4net using the specified configuration file.

public static ICollection Configure(FileInfo configFile)

Parameters

configFile FileInfo

The XML file to load the configuration from.

Returns

ICollection

Remarks

The configuration file must be valid XML. It must contain at least one element called log4net that holds the log4net configuration data.

The log4net configuration file can possible be specified in the application's configuration file (either MyAppName.exe.config for a normal application on Web.config for an ASP.NET application).

The first element matching <configuration> will be read as the configuration. If this file is also a .NET .config file then you must specify a configuration section for the log4net element otherwise .NET will complain. Set the type for the section handler to System.Configuration.IgnoreSectionHandler, for example:

The following example configures log4net using a configuration file, of which the location is stored in the application's configuration file :
using log4net.Config;
using System.IO;
using System.Configuration;

...

XmlConfigurator.Configure(new FileInfo(ConfigurationSettings.AppSettings["log4net-config-file"]));

In the .config file, the path to the log4net can be specified like this :

Configure(Stream)

Configures log4net using the specified configuration data stream.

public static ICollection Configure(Stream configStream)

Parameters

configStream Stream

A stream to load the XML configuration from.

Returns

ICollection

Remarks

The configuration data must be valid XML. It must contain at least one element called log4net that holds the log4net configuration data.

Note that this method will NOT close the stream parameter.

Configure(Uri)

Configures log4net using the specified configuration URI.

public static ICollection Configure(Uri configUri)

Parameters

configUri Uri

A URI to load the XML configuration from.

Returns

ICollection

Remarks

The configuration data must be valid XML. It must contain at least one element called log4net that holds the log4net configuration data.

The WebRequest must support the URI scheme specified.

Configure(XmlElement)

Configures log4net using a log4net element

public static ICollection Configure(XmlElement element)

Parameters

element XmlElement

The element to parse.

Returns

ICollection

Remarks

Loads the log4net configuration from the XML element supplied as element.

Configure(ILoggerRepository)

Automatically configures the ILoggerRepository using settings stored in the application's configuration file.

public static ICollection Configure(ILoggerRepository repository)

Parameters

repository ILoggerRepository

The repository to configure.

Returns

ICollection

Remarks

Each application has a configuration file. This has the same name as the application with '.config' appended. This file is XML and calling this function prompts the configurator to look in that file for a section called log4net that contains the configuration data.

To use this method to configure log4net you must specify the Log4NetConfigurationSectionHandler section handler for the log4net configuration section. See the Log4NetConfigurationSectionHandler for an example.

Configure(ILoggerRepository, FileInfo)

Configures the ILoggerRepository using the specified configuration file.

public static ICollection Configure(ILoggerRepository repository, FileInfo configFile)

Parameters

repository ILoggerRepository

The repository to configure.

configFile FileInfo

The XML file to load the configuration from.

Returns

ICollection

Remarks

The configuration file must be valid XML. It must contain at least one element called log4net that holds the configuration data.

The log4net configuration file can possible be specified in the application's configuration file (either MyAppName.exe.config for a normal application on Web.config for an ASP.NET application).

The first element matching <configuration> will be read as the configuration. If this file is also a .NET .config file then you must specify a configuration section for the log4net element otherwise .NET will complain. Set the type for the section handler to System.Configuration.IgnoreSectionHandler, for example:

The following example configures log4net using a configuration file, of which the location is stored in the application's configuration file :
using log4net.Config;
using System.IO;
using System.Configuration;

...

XmlConfigurator.Configure(new FileInfo(ConfigurationSettings.AppSettings["log4net-config-file"]));

In the .config file, the path to the log4net can be specified like this :

Configure(ILoggerRepository, Stream)

Configures the ILoggerRepository using the specified configuration file.

public static ICollection Configure(ILoggerRepository repository, Stream configStream)

Parameters

repository ILoggerRepository

The repository to configure.

configStream Stream

The stream to load the XML configuration from.

Returns

ICollection

Remarks

The configuration data must be valid XML. It must contain at least one element called log4net that holds the configuration data.

Note that this method will NOT close the stream parameter.

Configure(ILoggerRepository, Uri)

Configures the ILoggerRepository using the specified configuration URI.

public static ICollection Configure(ILoggerRepository repository, Uri configUri)

Parameters

repository ILoggerRepository

The repository to configure.

configUri Uri

A URI to load the XML configuration from.

Returns

ICollection

Remarks

The configuration data must be valid XML. It must contain at least one element called log4net that holds the configuration data.

The WebRequest must support the URI scheme specified.

Configure(ILoggerRepository, XmlElement)

Configures the ILoggerRepository using the specified XML element.

public static ICollection Configure(ILoggerRepository repository, XmlElement element)

Parameters

repository ILoggerRepository

The repository to configure.

element XmlElement

The element to parse.

Returns

ICollection

Remarks

Loads the log4net configuration from the XML element supplied as element.

ConfigureAndWatch(FileInfo)

Configures log4net using the file specified, monitors the file for changes and reloads the configuration if a change is detected.

public static ICollection ConfigureAndWatch(FileInfo configFile)

Parameters

configFile FileInfo

The XML file to load the configuration from.

Returns

ICollection

Remarks

The configuration file must be valid XML. It must contain at least one element called log4net that holds the configuration data.

The configuration file will be monitored using a FileSystemWatcher and depends on the behavior of that class.

For more information on how to configure log4net using a separate configuration file, see Configure(FileInfo).

See Also
Configure(FileInfo)

ConfigureAndWatch(ILoggerRepository, FileInfo)

Configures the ILoggerRepository using the file specified, monitors the file for changes and reloads the configuration if a change is detected.

public static ICollection ConfigureAndWatch(ILoggerRepository repository, FileInfo configFile)

Parameters

repository ILoggerRepository

The repository to configure.

configFile FileInfo

The XML file to load the configuration from.

Returns

ICollection

Remarks

The configuration file must be valid XML. It must contain at least one element called log4net that holds the configuration data.

The configuration file will be monitored using a FileSystemWatcher and depends on the behavior of that class.

For more information on how to configure log4net using a separate configuration file, see Configure(FileInfo).

See Also
Configure(FileInfo)