Log4Net不记录(可能未初始化) [英] Log4Net not logging (probably not initialised)

查看:71
本文介绍了Log4Net不记录(可能未初始化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次使用Log4Net时,我遇到了麻烦.我遵循了各种教程,但无法获取所有内容.让我向您展示我的代码,希望您能告诉我我做错了什么.

First time playing with Log4Net and I'm running into trouble. I've followed various tutorials but I've been unable to get it to log anything out. Let me show you my code and hopefully you can tell me what I'm doing wrong.

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" 
             type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>  
  <log4net>    
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="CurrentLog.txt"/>
      <staticLogFileName value="true"/>
      <appendToFile value="true"/>
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd"/>
      <filter type="log4net.Filter.LevelRangeFilter">
        <acceptOnMatch value="true" />
        <levelMin value="DEBUG" />
        <levelMax value="FATAL" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{dd MMM yyy HH:mm:ss} %level %message. %newline %exception" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingFileAppender" />
    </root>
  </log4net>
</configuration>

AssemblyInfo.cs

[assembly: log4net.Config.XmlConfigurator(Watch=true)]

Presenter.cs

在课堂上我有这个:

private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

然后,我稍后在该类中尝试使用log变量:

Then I try to use the log variable later in the class:

bool isDebugEnabled = log.IsDebugEnabled;
log.Debug("Failed to save", e);

每当我检查isDebugEnabled变量时,它都是错误的,如果我检查log变量,则其他所有isBlahEnabled也是错误的.

Whenever I inspect the isDebugEnabled variable, it is false, as are all of the other isBlahEnabled if I inspect the log variable.

我怀疑我没有正确连接我的app.config文件,因为这是我第一次尝试使用它.我通过在解决方案资源管理器中右键单击该项目,添加一个新项,选择应用程序配置文件"并将其命名为app.config来创建.

My suspicion is that I have not hooked up my app.config file correctly because this is the first time I have tried to use one. I created by right clicking on the project in solution explorer, adding a new item, choosing Application Configuration File and naming it app.config.

推荐答案

这个对我有用:

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    </configSections>
    <log4net>
      <appender name="Main" type="log4net.Appender.RollingFileAppender">
        <file value="${USERPROFILE}\My Documents\MyApp\Logs\Main.log" />
        <appendToFile value="false" />
        <maximumFileSize value="1GB" />
        <maxSizeRollBackups value="3" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date{ABSOLUTE} %-5level %-18logger %message%newline" />
        </layout>
      </appender>
      <root>
        <level value="DEBUG" />
        <appender-ref ref="Main" />
      </root>
    </log4net>
</configuration>

还要确保将app.config上的构建操作设置为,并将复制到输出目录"设置为如果更新则复制" .您可以在文件属性中设置这些设置.

Also be sure the build action on app.config is set to None and Copy to output dir is set to "Copy if newer". You can set these settings in the file properties.

Program.cs

public static ILog Log;

static void Main(string[] args)
{
    // Setup Logging
    log4net.Config.XmlConfigurator.Configure();
    Log = LogManager.GetLogger("MyAwesomeApp");

    // ...
}

这篇关于Log4Net不记录(可能未初始化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆