You are here

System.Configuration.ConfigurationErrorsException - Configuration system failed to initialize - Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element.

Submitted by Asif Nowaj, Last Modified on 2019-11-22

When you are working .NET application, whether that is a web application or console application or any other project type, you may face an exception called "ConfigurationErrorsException". The error message appears as Configuration system failed to initialize.

If you see the inner exception message it will say: Only one <configSections> element allowed per config file and if present must be the first child of the root element.

I faced a same problem once while working with a .NET console application and my app.config was something like below.


<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
    </startup>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <connectionStrings>
    <add name="Reportingdb" connectionString="Data Source=localhost;Initial Catalog=ReportToolDB; User Id=DBUser; password=DBUser;" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <log4net>
    <root>
      <level value="ALL"></level>
      <appender-ref ref="AdoNetAppender"></appender-ref>
    </root>
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <!-- ... -->
    </appender>
  </log4net>
  <appSettings>
    <!-- ... -->
  </appSettings>
</configuration>

I was trying to working with adding log4net for application logging purpose. I faced this issue after amending my app.config.

Resolution:

If you see the error message carefully, it says <configSections> must be the first child of the root <configuration> element. This is exactly the reason in this case. Move <configSection> element as first element of <configuration>. The issue should be resolved. So my modified configuration looks like and it resolved the problem.


<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
    </startup>
  <connectionStrings>
    <add name="Reportingdb" connectionString="Data Source=localhost;Initial Catalog=ReportToolDB; User Id=DBUser; password=DBUser;" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <log4net>
    <root>
      <level value="ALL"></level>
      <appender-ref ref="AdoNetAppender"></appender-ref>
    </root>
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <!-- ... -->
    </appender>
  </log4net>
  <appSettings>
    <!-- ... -->
  </appSettings>
</configuration>

Discussion or Comment

If you have anything in mind to share, please bring it in the discussion forum here.

https://forum.everyething.com/dotnet-f40/