log4net 不查看我的 app.config

发布于 2024-12-11 02:06:37 字数 1579 浏览 0 评论 0原文

我将 log4net 配置为监视对 app.config 文件所做的更改。

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

当我运行我的应用程序并更改配置文件中的内容时,这些更改仅在我重新启动应用程序时才会生效。为什么会这样呢?

还有一种方法可以告诉 log4net 监视 app.config 中的更改吗?就像:

<appender name="FileAppender" type="log4net.Appender.FileAppender" >
    <watch value="true" />
</appender>

------------- 编辑 -------------

我现在尝试使用单独的配置文件:log4net.config。
它看起来像这样:

<log4net>
  <appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="c:\log.txt" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d [%t] %-5p %c (line %L) -- %m%n" />
    </layout>
  </appender>
  <root>
    <appender-ref ref="FileAppender" />
  </root>
</log4net>

在我的 assemblyInfo.cs 中,我编写了以下内容:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

记录到文件的类看起来像这样:

ILog myLogger = LogManager.GetLogger(typeof(Form1));
myLogger.Debug("test");

这与旧版本类似。已创建日志文件条目,但是当我在运行时更改 log4net.config 时,这些更改不会应用......“Watch=true”应该启用该功能,对吗?

I configured my log4net to watch on changes made to the app.config file.

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

When I run my app and change things in the config file, these changes only take effect when I restart my app. Why could this be?

Is there also a way to tell log4net to watch on changes in the app.config? Like:

<appender name="FileAppender" type="log4net.Appender.FileAppender" >
    <watch value="true" />
</appender>

------------- EDIT -------------

I tried now to use a separate config-file: log4net.config.
It looks like this:

<log4net>
  <appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="c:\log.txt" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d [%t] %-5p %c (line %L) -- %m%n" />
    </layout>
  </appender>
  <root>
    <appender-ref ref="FileAppender" />
  </root>
</log4net>

In my assemblyInfo.cs I wrote the following:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

The class that logs to the file looks like this:

ILog myLogger = LogManager.GetLogger(typeof(Form1));
myLogger.Debug("test");

This works like the old version. logfile entries are made, but when I change my log4net.config during runtime, these changes are not applied.... "Watch=true" should enable that feature, right?

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

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

发布评论

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

评论(3

椵侞 2024-12-18 02:06:37

哈!,我刚刚遇到了同样的问题,正在运行需要日志记录的单元测试。
添加这一行修复了它:

log4net.Config.XmlConfigurator.Configure();

我的 App.config:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="log.txt" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="100KB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
    </appender>
    <root>
        <level value="DEBUG" />
        <appender-ref ref="RollingFileAppender" />
    </root>
</log4net>

我也有这个:

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

HA!, I was just encountering the same problem, running unit tests that require logging.
Adding this line fixed it:

log4net.Config.XmlConfigurator.Configure();

My App.config:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="log.txt" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="100KB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
    </appender>
    <root>
        <level value="DEBUG" />
        <appender-ref ref="RollingFileAppender" />
    </root>
</log4net>

I also do have this:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
你的呼吸 2024-12-18 02:06:37

根据 log4net 文档,监视功能不适用于应用程序配置文件(应用程序配置、网络配置):

因为System.Configuration API不支持重新加载配置文件
无法使用查看配置设置
log4net.Config.XmlConfigurator.ConfigureAndWatch 方法。

因此,如果您需要重新配置 log4net 配置,则需要将其放置在单独的 XML 文件中,并且您的应用程序需要具有足够的权限来读取该文件:

可以使用任何接受 System.IO.FileInfo 对象的 log4net.Config.XmlConfigurator 方法来指定从中读取配置的文件。由于可以监视文件系统的文件更改通知,因此可以使用ConfigureAndWatch 方法来监视配置文件的修改并自动重新配置log4net。

According to log4net documentation, the Watch feature does not work for application configuration files (app.config, web.config):

Because the System.Configuration API does not support reloading of the config file
the configuration settings cannot be watched using the
log4net.Config.XmlConfigurator.ConfigureAndWatch methods.

So if you need the log4net configuration to be re-configurable, you will need to place it in a separate XML file and your application needs to have sufficient permissions to read the file:

The file to read the configuration from can be specified using any of the log4net.Config.XmlConfigurator methods that accept a System.IO.FileInfo object. Because the file system can be monitored for file change notifications the ConfigureAndWatch methods can be used to monitor the configuration file for modifications and automatically reconfigure log4net.

梦亿 2024-12-18 02:06:37

尽管我来得太晚了 - 这对我有帮助:在程序的一开始简单地调用 log4net.LogManager.GetLogger("DUMMY"); 。我将它放在 program.csMain() 方法的第一行。无需将记录器分配给任何对象,只需礼貌地请求 log4net 读取程序集的属性,如所述 此处

使用属性可以是一种更清晰的方法来定义从何处加载应用程序的配置。但值得注意的是,属性纯粹是被动的。它们仅供参考。因此,如果您使用配置属性,则必须调用 log4net 以允许其读取属性。对 LogManager.GetLogger 的简单调用将导致调用程序集上的属性被读取和处理。 因此,必须在应用程序启动期间尽早进行日志记录调用,当然是在加载和调用任何外部程序集之前

Even though I'm terribly late to the party - here's, what helped me: a simple call to log4net.LogManager.GetLogger("DUMMY"); at the very beginning of my program. I put it in the very first line of program.cs's Main() method. No need to assign the logger to any object, merely a polite request to log4net to read the assembly's attributes as stated here.

Using attributes can be a clearer method for defining where the application's configuration will be loaded from. However it is worth noting that attributes are purely passive. They are information only. Therefore if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked.

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