Log4Net config file pre configured

The XmlConfigurator can directly read any XML file and use it to configure log4net. This includes the application's .config file; the file named MyApp.exe.config or Web.config. The only reason not to read the configuration file directly is if the application does not have sufficient permissions to read the file, then the configuration must be loaded using the .NET configuration APIs (see above).

The file to read the configuration from can be specified using any of the log4net.Config.XmlConfigurator methods that accept a System.IO.FileInfo object. Because the file system can be monitored for file change notifications the ConfigureAndWatch methods can be used to monitor the configuration file for modifications and automatically reconfigure log4net.

Additionally the log4net.Config.XmlConfiguratorAttribute can be used to specify the file to read the configuration from.

The configuration is read from the log4net element in the file. Only one log4net element can be specified in the file but it may be located anywhere in the XML hierarchy. For example it may be the root element:


<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%date : %method ~ [%line] - MSG: %message%newline%exception" />
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="app-logs/todays-log.log" />
<appendToFile value="true" />
<maximumFileSize value="40MB" />
<maxSizeRollBackups value="5" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date : %method ~ [%line] - MSG: %message%newline%exception" />
</layout>
</appender>
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date : %method ~ [%line] - MSG: %message%newline%exception" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ManagedColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="Red" />
</mapping>
<mapping>
<level value="WARN" />
<foreColor value="Yellow" />
</mapping>
<mapping>
<level value="INFO" />
<foreColor value="White" />
</mapping>
<mapping>
<level value="DEBUG" />
<foreColor value="Green" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date : %method ~ [%line] - MSG: %message%newline%exception" />
</layout>
</appender>
<root>
<level value="TRACE" />
<appender-ref ref="RollingFile" />
<appender-ref ref="TraceAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>

Summary:

log4net includes a configuration reader that parses an XML DOM, the log4net.Config.XmlConfigurator. This section defines the syntax accepted by the configurator.