Shibin V.M
Shibin V.M

Reputation: 431

log4Net log file not writing

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

Upvotes: 8

Views: 21704

Answers (2)

Rustem
Rustem

Reputation: 326

Try to give write permission in E:/Log for asp.net user or for Everyone, then try to add requirePermission="false" attribute, like this:

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" requirePermission="false" />

or you should specify logging level in root section, like this:

<root>
  <level value="ALL" />
  <appender-ref ref="RollingFileAppender" />
</root>

here ASP.NET 3.5 – File Appender sample application

Upvotes: 5

Milen Kindekov
Milen Kindekov

Reputation: 1973

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

See this solution: Configure Log4Net in web application

Upvotes: 10

Related Questions