我的App.config
文件如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<log4net>
<!-- A1 is set to be a ConsoleAppender -->
<appender name="A1" type="log4net.Appender.FileAppender">
<file value="logfile.txt" />
<appendToFile value="false" />
<!-- A1 uses PatternLayout -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
<level value="DEBUG" />
<appender-ref ref="A1" />
</root>
</log4net>
</configuration>
执行测试程序最终会出现以下错误消息:
Executing the test program ends up in the following error message:
log4net:ERROR Exception while reading ConfigurationSettings. Check your .config
file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed t
o initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognize
d configuration section log4net.
我的配置文件出了什么问题?
What is wrong with my configuration file?
更新1:
configSections´ part was missing, as pointed out in the accepted answer. But I also had to remove the
startup section, otherwise the same error appeared. I do not know why the
startup`部分也引起了该问题.也许更有经验的人可以说并写评论.
UPDATE 1:
The configSections´ part was missing, as pointed out in the accepted answer. But I also had to remove the
startupsection, otherwise the same error appeared. I do not know why the
startup` section is causing the problem, too. Perhaps someone more experienced can tell and write a comment.
推荐答案
您还需要在节块中添加log4net
You need to add log4net
also in the section block
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
这篇关于为什么在配置文件中无法识别log4net?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!