log4net 不查看我的 app.config
我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
哈!,我刚刚遇到了同样的问题,正在运行需要日志记录的单元测试。
添加这一行修复了它:
我的 App.config:
我也有这个:
HA!, I was just encountering the same problem, running unit tests that require logging.
Adding this line fixed it:
My App.config:
I also do have this:
根据 log4net 文档,监视功能不适用于应用程序配置文件(应用程序配置、网络配置):
因此,如果您需要重新配置 log4net 配置,则需要将其放置在单独的 XML 文件中,并且您的应用程序需要具有足够的权限来读取该文件:
According to log4net documentation, the Watch feature does not work for application configuration files (app.config, web.config):
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:
尽管我来得太晚了 - 这对我有帮助:在程序的一开始简单地调用
log4net.LogManager.GetLogger("DUMMY");
。我将它放在 program.cs 的 Main() 方法的第一行。无需将记录器分配给任何对象,只需礼貌地请求 log4net 读取程序集的属性,如所述 此处。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.