Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 520
Shibin V.M Avatar asked Nov 23 '12 08:11

Shibin V.M


People also ask

What is rolling file Appender in log4net?

RollingFileAppender can roll log files based on size or date or both depending on the setting of the RollingStyle property. When set to Size the log file will be rolled once its size exceeds the MaximumFileSize.

Where are log4net logs stored?

The log4net. config file is located in the \Enterprise7\bin\ directory and is the configuration file for the log4net logging application.

Does log4net support .NET core?

Yes, you can still use Log4Net to help you output log statements of text files, databases, and other destinations using . NET Core 6 by installing log4net version 2.0. 7 (this version targets . NET Standard) and beyond via NuGet Packages.


2 Answers

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

See this solution: Configure Log4Net in web application

like image 74
Milen Kindekov Avatar answered Oct 14 '22 13:10

Milen Kindekov


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

like image 45
Rustem Avatar answered Oct 14 '22 14:10

Rustem