外部文件中的 Log4Net 配置不起作用 [英] Log4Net config in external file does not work
问题描述
我们正在使用 log4net 并希望在外部配置文件中指定它的配置(就像我们在其他部分所做的那样).为此,我们将 App.config 中的 log4net 部分更改为:
We are using log4net and want to specify it's configuration in an external config file (as we have done with other sections). To do this we have changed the log4net section in the App.config to:
...
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
...
<log4net configSource="Log.config" />
...
在 Log.Config 文件(与 App.config 相同的目录)中,我们有:
And in the Log.Config file (same directory as the App.config) we have:
<log4net>
<appender name="General" type="log4net.Appender.FileAppender">
<file value="myapp.log" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<root>
<appender-ref ref="General" />
</root>
</log4net>
然而,当我们运行应用程序时,没有创建日志文件(也没有完成日志记录).没有错误消息输出到控制台.
However, when we run the app, no log file is created (and no logging done). There are no error messages output to the console.
如果我们将 Log.config 文件的内容移回 App.config(替换上面的第一行代码),它会按预期工作.知道为什么它不能在外部文件中工作吗?
If we move the contents of the Log.config file back into the App.config (replacing the first code line above), it works as expected. Any idea why it is not working in an external file?
推荐答案
你的 AssemblyInfo.cs
文件中是否有以下属性:
Do you have the following attribute in your AssemblyInfo.cs
file:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
在每个需要日志功能的类的开始处这样的代码:
and code like this at the start of each class that requires logging functionality:
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
我有一篇包含此信息和其他信息的博客文章 这里.
I have a blog post containing this and other info here.
这篇关于外部文件中的 Log4Net 配置不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!