简体   繁体   中英

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.

我无法在Asembly.cs中设置工作而不将其添加到Global.asax中的Application_Start:

log4net.Config.XmlConfigurator.Configure();

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.

所以你正在加载配置文件,但是你是否正在设置日志变量

private static readonly ILog log = LogManager.GetLogger(typeof(Program));

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM