log4net配置异常

18

我正在使用log4net进行日志记录。我的日志配置存储在一个单独的文件中。

Web.Config:ConfigSections

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

如何在AssemblyInfo.cs文件中指定我的配置文件

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

当我初始化我的LogManager时,我遇到了这个错误

"System.TypeLoadException"
message: Could not load type 'log4net.Config.Log4NetConfigurationSectionHlandler' from assembly 'Log4net'.

是的,它确实写着"Log4NetConfigurationSectionHlandler'",这不是拼写错误。

后来出现了这个错误。

An error occurred creating the configuration section handler for log4net: Could not load type 'log4net.Config.Log4NetConfigurationSectionHlandler' from assembly 'Log4net'. 

编辑:尝试了Mauricio Scheffer的建议

得到了

log4net:ERROR XmlConfigurator: Failed to find configuration section 'log4net' in the application's .config file. Check your .config file for the <log4net> and <configSections> elements. The configuration section should look like: <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

请配置log4net与XML文件。 - Mauricio Scheffer
编辑:我刚发现我的web.config文件中有一个类型“Log4NetConfigurationSectionHlandler”。我仍在努力让它与外部配置文件一起工作。 - ram
1个回答

27

如果你将配置文件单独存放在log4net.config文件中,则不需要使用sectionHandler。请移除它。

你也可能在代码中某处调用了XmlConfigurator.Configure()方法。请将其也移除。

此外,还可参考这篇文章


1
尝试此建议时,我遇到了以下错误:log4net:ERROR XmlConfigurator:在应用程序的.config文件中未找到配置部分“log4net”。请检查您的.config文件中的<log4net>和<configSections>元素。配置部分应如下所示:<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />。 - ram
1
@ram: 你可能在代码的某个地方调用了XmlConfigurator.Configure()。移除它即可。 - Mauricio Scheffer
4
没错,你不应该调用它。汇编特性已经处理了初始化。 - Mauricio Scheffer
谢谢Mauricio,它起作用了。我很快会发布一个新问题。它是关于在程序集中使用一个类来记录所有事件,我看到“logger”被设置为这个静态日志记录类,而不是调用这些Log方法的类。 - ram
@Darren:日志配置绝不能中定义。这是应用程序的责任。 - Mauricio Scheffer
显示剩余8条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接