7

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.

Community
  • 1
  • 1
Dotnet_user
  • 71
  • 1
  • 2

4 Answers4

7

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();
jvilalta
  • 6,679
  • 1
  • 28
  • 36
1

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.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
1

So you are loading the configuration file but are you then setting the log variable like so

private static readonly ILog log = LogManager.GetLogger(typeof(Program));
etoisarobot
  • 7,684
  • 15
  • 54
  • 83
0

Unless you specify a full path for the separate config file log4net expects the file to be in the path returned by:

 System.AppDomain.CurrentDomain.BaseDirectory

If you have a simple console application, this usually means the directory containing the assembly but this is not necessarily true in other cases. There are many ways to solve this problem (c.f. my answer here as well).

To start it might be easier to put the log4net configuration back to the app/web.config and ignore the schema warning. If logging works then you can be sure you have no permission issues and can move to the next step to have the configuration in an external file.

What kind of application are you writing? Maybe I could be a bit more specific with my answer...

Community
  • 1
  • 1
Stefan Egli
  • 17,398
  • 3
  • 54
  • 75
  • Thank you for the response. I was working with a service. The issue is resolved now. Apparently there was some problem in configuring the service itself. Once that was fixed, everything else worked. – Dotnet_user Apr 29 '10 at 14:57