Logging 将log4net配置添加到app.config时出现ConfigurationErrorsException

Logging 将log4net配置添加到app.config时出现ConfigurationErrorsException,logging,log4net,appsettings,Logging,Log4net,Appsettings,我正在测试log4net;它确实工作得很好。但是,当我将log4net配置添加到应用程序的app.config中时,它会抛出一个ConfigurationErrorsException。这是我的app.config文件: <?xml version="1.0" encoding="utf-8" ?> <configuration> <configsections> <section name="log4net" type="log4net.C

我正在测试log4net;它确实工作得很好。但是,当我将log4net配置添加到应用程序的app.config中时,它会抛出一个
ConfigurationErrorsException
。这是我的app.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configsections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,     log4net" />
  </configsections>
  <log4net>
    <!-- Set root logger level to DEBUG and its only appender to A1 -->
    <root>
      <level value="ERROR" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <file value="c:\log\" />
      <datePattern value="yyyy-MM-dd'_siteAgent.log'" />
      <staticLogFileName value="false" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="5MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] -     %message%newline" />
      </layout>
    </appender>
  </log4net>
  <appSettings>
    <add key ="test" value="tttt"/>
  </appSettings>
</configuration>    
异常发生在下一行

string t = ConfigurationManager.AppSettings["test"].ToString();
您的
节点必须是web.config中的
节点,否则配置文件无效

如果configSections元素位于配置文件中,则 configSections元素必须是的第一个子元素 配置元素


我将configsectins移到了第一个子级,但仍然得到了相同的错误。我还更改了我的问题配置。始终发布完整的异常消息和堆栈跟踪。
string t = ConfigurationManager.AppSettings["test"].ToString();