Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net logging not creating log file

I'm not sure if this is the right forum to post this question. But I'm just hoping someone here might have used log4net in the past, so hoping to get some help.

I'm using log4net to log my exceptions. The configuration settings look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configSections>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
 </configSections>
 <log4net debug="false">
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
   <file value="C:\Logs\sample.log" />
   <appendToFile value="true"/>
   <rollingStyle value="Size"/>
   <maxSizeRollBackups value="10"/>
   <maximumFileSize value="10MB"/>
   <staticLogFileName value="true"/>
   <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%-5level %date %logger.%method[line %line] - %message%newline"/>
   </layout>
 </appender>
 <root>
  <level value="INFO"/>
  <appender-ref ref="RollingLogFileAppender"/>
 </root>
</log4net>
</configuration>

I started out by adding this configuration to web.config, but I got an error (VS studio could not find a schema for log4net-"Could not find schema information for the element log4net"). So I followed this link (Log4Net "Could not find schema information" messages) and configured my settings in a separate xml file and added the following line of code in my AssemblyInfo.cs:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "xmlfile.xml", Watch = true)]

And in the actual code, I placed this line:

public void CreateUser(String username, String password)
{
 try
 {
  log.Info("Inside createuser");
  //code for creating user
 }
 catch(exception e)
 {
  log.Info("something happened in create user", e);
 }
}

The problem is that the log file is not being created. I can't see anything inside C:\Logs. Can anybody tell me what I'm doing wrong here?

Any suggestions/inputs will be very helpful.

Thank you all in advance.

like image 276
Dotnet_user Avatar asked Apr 27 '10 15:04

Dotnet_user


2 Answers

I could not get the setting in Asembly.cs to work without adding this to Application_Start in my Global.asax:

log4net.Config.XmlConfigurator.Configure();
like image 52
jvilalta Avatar answered Oct 17 '22 12:10

jvilalta


Your log file is not being created probably because the ASPNET user does not have permission to write to the C:\Logs\ directory. Make sure you give full rights to ASPNET to wherever your applicaton wants to create the log file.

like image 21
Otávio Décio Avatar answered Oct 17 '22 12:10

Otávio Décio