【发布时间】:2013-08-18 03:17:40
【问题描述】:
我是第一次使用 log4net,我不知道如何添加适当的配置设置。关于在 app.config 文件中添加 <log4net> 部分的所有文档都非常一致,但是为了正确编译,我是否还需要概述我的 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 的类型。)
【问题讨论】:
-
为什么要重新声明“system.serviceModel”、“appSettings”和“startup”?它们已经在更高级别(machine.config?)正确声明,通常您不需要在这里重新声明它们。你也有 在配置文件的开头?
-
我同意史蒂夫的观点,您需要声明的唯一配置部分是 log4net。删除所有其他的,并确保
<?xml version="1.0"?>位于最顶部。如果出于某种奇怪的原因您必须声明它们,那么您必须指定它们的type属性。
标签: c# xml logging log4net config