Aug 28, 2018

Using log4net in ASP.Net web application

Instead of keeping a separate log4net.config file, we could move the log4net.config inside the web.config of the ASP.Net application. E.g.:-
<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <rollingStyle value="Composite" />
      <file value="D:\logs\mylogs_"  type="log4net.Util.PatternString"/>
      <datePattern value="yyyy-MM-dd'.log'" />
      <staticLogFileName value="false" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <appendToFile value="true" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
    </appender>
  </log4net>
  <appSettings></appSettings>
  <system.web></system.web>
</configuration>

The code to initialize the log4net can then be placed in a separate class file as:
public class Logger
{
    public readonly static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    static Logger ()
    {
        log4net.Config.XmlConfigurator.Configure();
    }
}
Note that the log variable and the constructor are static. This makes sure that log4net is available as soon as the application is initialized. To log something you may use the code as:
Logger.log.Error("Error Message");


If you do not want to keep config for log4net in web.config, create a separate log4net.config file at some location. The file can be created as:
<log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <rollingStyle value="Composite" />
      <file value="D:\logs\ProductAttributes_"  type="log4net.Util.PatternString"/>
      <datePattern value="yyyy-MM-dd'.log'" />
      <staticLogFileName value="false" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <appendToFile value="true" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
    </appender>
</log4net>

The code to initialize the log4net can then be placed in a separate class file as:
public class Logger
{
    public readonly static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    static Logger ()
    {
        /// setting log4net config file path programatically
        /// var log4ConfigPath = AppDomain.CurrentDomain.BaseDirectory + "Logger\\log4net.config";
        /// setting log4net config file path
        var log4ConfigPath = "C:\\Logger\\log4net.config"; //log4net.config location

        log4net.Config.XmlConfigurator.Configure();
    }
}
To log something you may use the code as mentioned above:
Logger.log.Error("Error Message");