我使用log4net在C#中编写了一个测试控制台应用程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using log4net.Config;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Log4Net_Test
{
class Program
{
private static readonly ILog log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
log.Info("Entering application");
log.Debug("Debug message");
log.Info("Leaving application");
}
}
}
我的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>
执行测试程序最终会出现以下错误消息:
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.
我的配置文件有什么问题?
更新1:
configSections´ part was missing, as pointed out in the accepted answer. But I also had to remove the
启动section, otherwise the same error appeared. I do not know why the
启动`部分也导致了问题。也许更有经验的人可以讲述并撰写评论。
答案 0 :(得分:35)
您还需要在区块
中添加log4net
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
答案 1 :(得分:13)
启动部分也需要删除,因为 log4net 不支持 Framework .NET4.5
您可以在此处看到我的类似答案:https://stackoverflow.com/a/13236410/1783224
或者您可以在文档中看到支持的框架:http://logging.apache.org/log4net/release/features.html
答案 2 :(得分:4)
您还应该记住部分块&lt; configSections &gt;必须在&lt; 配置&gt;下否则会发生错误:
读取ConfigurationSettings时出现异常。检查.config文件 是格式良好的XML。
例如:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<!-- Log4net Logging Setup -->
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
<file value="service.log" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<!--
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
-->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger.%method - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>