c# - 配置文件中的节类型

标签 c# xml logging log4net config

我是第一次使用 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.serviceModelstartup )
  • Could not find schema information for the element * (*=log4net 中的所有内容)

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

我应该使用单独的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/

相关文章:

elasticsearch - 使用 Watcher 和 Kibana 监控电子邮件日志,在最后一分钟内每分钟触发一次

logging - 我可以使用 Jenkins 日志解析器隐藏控制台行吗

c# - 我最终采用了什么设计模式?

c# - 无参catch和其他catch的区别

sql - 使用 FOR XML PATH 从 XMLTYPE 列中选择时如何删除列名

c# - 使用 C# 读取具有嵌套命名空间的 XML

c# - Asp.NET MVC 中的强类型 View 与普通 View 与局部 View

c# - FakeItEasy 断言方法调用方法

C# XMLTextWriter - XML 1.1

c# - 重复代码位的设计模式/C#技巧