问题描述
我们正在使用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)]
日志功能:
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
我有一个包含此信息和其他信息的博客。
I have a blog post containing this and other info here.
这篇关于在外部文件中的log4Net配置不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!