C# – log4Net log file not writing

asp.netclog4netlogging

I am trying to implement log4net in my asp.net web application. But unfortunately the file is not created. Below is my configuration.

1 . Added log4net .dll reference

2 . Web.config settings.

  <configSections>
    <section name="log4net"type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>
  <log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="E:/Log/error_%date{dd-MM-yyyy}.log"/>
      <appendToFile value="true"/>
      <rollingStyle value="Date"/>
      <!--<maxSizeRollBackups value="5"/>
      <maximumFileSize value="10MB"/>
      <staticLogFileName value="true"/>-->
      <datePattern value="yyyyMMdd" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception%newline%newline"/>
      </layout>
    </appender>
    <root>
      <appender-ref ref="RollingFileAppender"/>
    </root>

  </log4net> 

3 . Added Assembly reference

     [assembly: log4net.Config.XmlConfigurator(Watch = true)]

4 . Log writing in the code behind

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

try
{
    throw new System.IO.FileNotFoundException();
}
catch (Exception ex)
{     
    log.Error("Error error logging", ex);       
}

These are the steps I had followed, but the log is not created…

Please give your suggestions.

Thanks in advance

Best Answer

Make sure you are calling the Configure function of the XmlConfigurator.

See this solution: Configure Log4Net in web application