Dan Snell
Dan Snell

Reputation: 2245

Why does section order matter in the app.config

I recently added log4net to my application. What struck me as rather odd was that I had to have the <configSections> tag block ahead of the <startup> tag block. If I didn't I ended up getting a really strange exception on the first db call I made. I am assuming there is a reason for this, but it seems rather strange.

This is the working example:

<?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.5" />
  </startup>


  <log4net debug="false">
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <param name="File" value="Logs\log.txt"/>
      <param name="AppendToFile" value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <!--<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] &lt;%X{auth}&gt; - %m%n"/>-->
        <conversionPattern value="%date - %-5level- %message%newline" />
      </layout>
    </appender>
    <root>
      <priority value="ALL"/>
      <appender-ref ref="LogFileAppender"/>
    </root>
    <category name="testApp.LoggingExample">
      <priority value="ALL"/>
    </category>
  </log4net>

Upvotes: 3

Views: 1163

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292505

The order of sections doesn't matter in general, it's only the configSections element that must be first.

From MSDN:

If the configSections element is in a configuration file, the configSections element must be the first child element of the configuration element.

I assume this is because sections need to be declared before they can be used.

Upvotes: 4

Related Questions