简体   繁体   English

log4net日志记录未创建日志文件

[英]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. 但我只是希望有人在这里过去曾经使用过log4net,所以希望得到一些帮助。

I'm using log4net to log my exceptions. 我正在使用log4net来记录我的异常。 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"). 我首先将此配置添加到web.config,但是我收到了一个错误(VS studio无法找到log4net的架构 - “无法找到元素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 : 所以我按照这个链接( Log4Net“找不到架构信息”消息 )并在一个单独的xml文件中配置我的设置,并在我的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. 我在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. 您的日志文件未创建可能是因为ASPNET用户没有写入C:\\Logs\\目录的权限。 Make sure you give full rights to ASPNET to wherever your applicaton wants to create the log file. 确保您将ASPNET的完全权限授予您的应用程序想要创建日志文件的任何位置。

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

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: 除非您为单独的配置文件指定完整路径,否则log4net要求文件位于由以下位置返回的路径中:

 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 . 要启动它可能更容易将log4net配置放回app / web.config并忽略架构警告 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... 也许我可以对我的回答更具体一点......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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