log4net 不会从 app.config 读取

发布于 2024-09-15 18:37:09 字数 1491 浏览 2 评论 0原文

我有两个为 log4net 配置相同的项目。一个项目记录良好;然而,另一个根本不记录。

不进行日志记录的项目中的 Logger 返回 IsFatalEnabled = falseIsErrorEnabled = falseIsWarnEnabled = falseIsInforEnabled = falseIsDebugEnabled = false

我已从一个项目复制并粘贴到另一个项目,完全替换该文件并尝试删除所有空白。

是什么导致一个项目无法正确从 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="FileAppender" type="log4net.Appender.FileAppender">
      <file value="logfile.txt" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date: %-5level – %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="FileAppender" />
    </root>
  </log4net>
</configuration>

Program.cs

using log4net;

class Program
{
    private static readonly ILog Log = LogManager.GetLogger("SO");

    static void Main(string[] args)
    {
        Log.Info("SO starting");
    }
}

I have two projects configured identically for log4net. One project logs fine; however, the other does not log at all.

The Logger in the project that is not logging returns IsFatalEnabled = false, IsErrorEnabled = false, IsWarnEnabled = false, IsInforEnabled = false and IsDebugEnabled = false.

I've copied and pasted from one project to the other, replaced the file completely and tried removing all whitespace.

What could be causing the one project not to properly be reading the correct levels from the 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="FileAppender" type="log4net.Appender.FileAppender">
      <file value="logfile.txt" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date: %-5level – %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="FileAppender" />
    </root>
  </log4net>
</configuration>

Program.cs

using log4net;

class Program
{
    private static readonly ILog Log = LogManager.GetLogger("SO");

    static void Main(string[] args)
    {
        Log.Info("SO starting");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彼岸花似海 2024-09-22 18:37:09

app.config 文件似乎未配置为由 log4net 监视。

我将以下行添加到 AssemblyInfo.cs 中,并且日志记录现已启用:

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

奇怪,因为我没有将此行添加到正在运行的项目中。

编辑:

看起来正在运行的项目毕竟有这条线。我一定是忘记了。

// This will cause log4net to look for a configuration file 
// called [ThisApp].exe.config in the application base 
// directory (i.e. the directory containing [ThisApp].exe) 
// The config file will be watched for changes. 
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

It seems that the app.config file was not configured to be watched by log4net.

I added the following line to AssemblyInfo.cs and logging is now enabled:

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

Weird, since I didn't add this line to the the project that was working.

EDIT:

Looks like the project that was working did have the line after all. I must have forgotten.

// This will cause log4net to look for a configuration file 
// called [ThisApp].exe.config in the application base 
// directory (i.e. the directory containing [ThisApp].exe) 
// The config file will be watched for changes. 
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
失退 2024-09-22 18:37:09

我的解决方案是在程序的启动代码中使用 XmlConfigurator.Configure() 。但是,我的情况有所不同,因为我使用 Autofac.log4net 模块在 Autofac 容器中注册 ILog 。在本例中,使用 XmlConfiguratorAttribute(将 Watch 设置为 true 或 false)使记录器保持未配置状态。

The solution for me was to use XmlConfigurator.Configure() in the program's startup code. However, my situation differed in that I was using the Autofac.log4net module to register the ILog in an Autofac container. In this instance, using XmlConfiguratorAttribute (with Watch set to either true or false) left the logger unconfigured.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文