我是第一次使用 log4net,我不知道如何添加适当的配置设置。关于添加 <log4net>
,所有文档都非常一致app.config 文件的部分,但要使其正确编译,我是否还需要概述我的 configSections
?
我现在有以下内容:
<configuration>
<configSections>
<section name="system.serviceModel"/>
<section name="appSettings" type="System.Configuration.ConfigurationManager"/>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<section name="startup" />
</configSections>
<system.serviceModel>
...
</system.serviceModel>
<appSettings>
...
</appSettings>
<log4net>
...
</log4net>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
</configuration>
但我收到以下错误:
-
XML document must contain a root level element
-
The required attribute 'type' is missing
(来自system.serviceModel
和startup
) -
Could not find schema information for the element *
(*=log4net 中的所有内容)
我已经阅读了一些关于分区组的帖子,并且我考虑过设置 appSettings
和 log4net
在单独的配置文件中。这有点让我头疼。
我应该使用单独的config 文件吗?
如果我应该将所有内容都放在一个config 文件中,我怎么知道一个部分是什么类型? (我根据我用来获取设置的程序集猜测 appSettings
类型——我从包括它在内的许多帖子中得到了 log4net
的类型。)
最佳答案
删除 configSections 中 "appSettings", "system.serviceModel", "startup"
的重复声明.
它们已在框架安装的文件 machine.config 中声明,位于 C:\WINDOWS\Microsoft.Net 的相应子文件夹中
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4Net" />
</configSections>
<appSettings>
....
</appSettings>
<log4net>
<root>
.....
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
.....
</appender>
</log4net>
<system.serviceModel>
....
</system.serviceModel>
</configuration>
还要确保您的配置文件以 <?xml version="1.0"?>
开头
关于c# - 配置文件中的节类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18261069/