views:

218

answers:

6

Hello everyone,

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 (http://stackoverflow.com/questions/174430/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.

+1  A: 

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
A: 

Hi,

Thank you for the response.

The user has full permission over that directory.

Could there be anything else?

Dotnet_user
+3  A: 

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();
TskTsk
this would require the configuration to be in app/web.config
Stefan Egli
No necessarily. That method has several overloads you could use.
TskTsk
I see, I had the impression you suggested to use the overload without parameters...
Stefan Egli
Addng this line of code gives me an error.
Dotnet_user
A: 

I tried to add that piece of code, but I got this error:

'log4net.Config.XMLConfigurator.Configure(log4net.Repository.ILoggerRepository,System.IO.Stream)' is a method but is used like a 'type'

Dotnet_user
Are you missing the "()" at the end?
s_hewitt
You should edit your initial question or post comments instead of posting followups as answers.
s_hewitt
Sorry about that.I'm not missing '()' at the end.
Dotnet_user
+1  A: 

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));
TooFat
Yes I am doing this
Dotnet_user
A: 

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...

Stefan Egli
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