外部文件中的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文件中(与应用程序相同的目录) 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="log.txt" />
<layout type="log4net.Layout.SimplyLayout" />
</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
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屋!