【问题标题】:Section types in Config files配置文件中的节类型
【发布时间】: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.serviceModelstartup
  • Could not find schema information for the element *(*=log4net 中的所有内容)

我已经阅读了一些关于部分组的帖子,并且我考虑在单独的配置文件中设置 appSettingslog4net。这有点过头了。

我应该使用单独的 config 文件吗?

如果我应该将所有内容放在一个 config 文件中,我怎么知道一个部分是什么类型? (我根据我用来获取设置的程序集猜测appSettings 类型——我从包括它在内的许多帖子中得到log4net 的类型。)

【问题讨论】:

  • 为什么要重新声明“system.serviceModel”、“appSettings”和“startup”?它们已经在更高级别(machine.config?)正确声明,通常您不需要在这里重新声明它们。你也有 在配置文件的开头?
  • 我同意史蒂夫的观点,您需要声明的唯一配置部分是 log4net。删除所有其他的,并确保&lt;?xml version="1.0"?&gt; 位于最顶部。如果出于某种奇怪的原因您必须声明它们,那么您必须指定它们的 type 属性。

标签: c# xml logging log4net config


【解决方案1】:

删除 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>

还要确保您的配置文件以 &lt;?xml version="1.0"?&gt; 开头

【讨论】:

  • 试图尽可能地模仿这一点。我确实有 xml 标头,尽管我在副本中错过了它。仍然收到XML document must contain a root level element 和有关未找到元素的所有消息的错误。还注意到您的回复不包括&lt;startup&gt;。这是故意的吗?
  • 不,只是从我的配置中复制/粘贴。那里没有启动。
  • 我建议尝试从配置中删除部分并检查错误何时消失。可能您在文件中的某处有问题。顺便问一下,您还有其他配置简单的项目可以正常工作吗?
  • 一切正常,直到我开始尝试添加 log4net 的东西。所以我注释掉了除了之前的所有内容(system.serviceModelstartup),在startup 上我得到了Could not find schema information 的所有内容。
  • 把项目带回家,在 VS2012 (Windows 8) 中加载并构建它,今天把它带回来,一切正常。不知道为什么。将您的输入标记为彻底的答案。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2018-07-01
  • 2023-01-31
  • 1970-01-01
  • 2013-07-09
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
  • 2013-11-08
相关资源
最近更新 更多