Dynamic Configuration of Log4Net

Visual Studio

I needed to dynamically configure log4net – outside of the web.config file.  Here is my solution:

1.  All that configuration of log4net in web.config?  Forget it.

2.  Create a separate log4net.config file (name it whatever you want), with the usual configuration content in it defining whatever appenders you want.   For example if you want a rolling file log:

XML
<log4net>
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
   <param name="File" value="C:\\Temp\\mylog.txt" />
   <param name="AppendToFile" value="true" />
   <param name="MaxSizeRollBackups" value="10" />
   <param name="MaximumFileSize" value="5MB" />
   <param name="RollingStyle" value="Size" />
   <param name="StaticLogFileName" value="true" />
   <layout type="log4net.Layout.PatternLayout,log4net">
   <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
  </layout>
</appender>
  <root>
   <level value="DEBUG" />
   <appender-ref ref="RollingLogFileAppender" />
  </root>
</log4net>
XML

As another example, here is a config file for both a rolling log file AND a SQL server log:

XML
<log4net>
   <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
    <param name="File" value="C:\\Temp\\mylog.txt" />
    <param name="AppendToFile" value="true" />
    <param name="MaxSizeRollBackups" value="10" />
    <param name="MaximumFileSize" value="5MB" />
    <param name="RollingStyle" value="Size" />
    <param name="StaticLogFileName" value="true" />
    <layout type="log4net.Layout.PatternLayout,log4net">
      <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
    </layout>
   </appender>
   <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
     <bufferSize value="100" />
     <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
     <connectionString value="Data Source=SERVER;Initial Catalog=DB1;User ID=USER1;Password=MYPW" />
     <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
     <parameter>
       <parameterName value="@log_date" />
       <dbType value="DateTime" />
       <layout type="log4net.Layout.RawTimeStampLayout" />
     </parameter>
     <parameter>
       <parameterName value="@thread" />
       <dbType value="String" />
       <size value="32" />
       <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%t" />
       </layout>
     </parameter>
     <parameter>
       <parameterName value="@log_level" />
       <dbType value="String" />
       <size value="512" />
       <layout type="log4net.Layout.PatternLayout">
         <conversionPattern value="%p" />
       </layout>
     </parameter>
     <parameter>
       <parameterName value="@logger" />
       <dbType value="String" />
       <size value="512" />
       <layout type="log4net.Layout.PatternLayout">
         <conversionPattern value="%c" />
       </layout>
     </parameter>
     <parameter>
       <parameterName value="@message" />
       <dbType value="String" />
       <size value="4000" />
       <layout type="log4net.Layout.PatternLayout">
         <conversionPattern value="%m" />
       </layout>
     </parameter>
     <parameter>
       <parameterName value="@exception" />
       <dbType value="String" />
       <size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ADONetAppender" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
XML

3.  Ok, so the good part now…  To load this log4net config file, add the following to your Global.asax.cs in the Application_Start function:

C#
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "log4net.config"));
log4net.LogManager.GetLogger("Global").Info("Application Started, Log4Net Configuration file loaded successfully.");
C#

This will load the config file into log4net, and go ahead and log one “info” item indicating that the configuration file was loaded successfully!

Leave a Reply

Your email address will not be published. Required fields are marked *