Home > Uncategorized > System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize —> System.Configuration.ConfigurationErrorsException: Only one element allowed per config file and if present must be the first child of the root element.

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


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

Solution:

Error app.config

——–

<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>
<appSettings>
<add key=”AA” value=”log4net.config” />
</appSettings>

<startup>
<supportedRuntime version=”v4.0″ sku=”.NETFramework,Version=v4.0″/>
</startup>

<!– <log4net configSource=”log4net.config” /> –>
<configSections>
<section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler, log4net” />
</configSections>
<log4net>
<appender name=”RollingFileAppender” type=”log4net.Appender.RollingFileAppender”>
<File value=”File1.log” />
<AppendToFile value=”true” />
<rollingStyle value=”Composite” />
<maximumFileSize value=”200MB” />
<layout type=”log4net.Layout.PatternLayout”>
<ConversionPattern value=”%date %messge%newline”/>
</layout>
</appender>

<appender name=”LogFileAppender” type=”log4net.Appender.RollingFileAppender”>
<File value=”File2.log” />
<AppendToFile value=”true” />
<rollingStyle value=”Date” />
<maximumFileSize value=”100MB” />
<layout type=”log4net.Layout.PatternLayout”>
<ConversionPattern value=”%date %messge%newline”/>
</layout>

<filter type=”log4net.Filter.LevelRangeFilter”>
<acceptOnMatch value=”true” />
<levelMin value=”INFO” />
</filter>
<filter type=”log4net.Filter.LevelRangeFilter”>
<acceptOnMatch value=”true” />
<levelMin value=”FATAL” />
</filter>
</appender>

<appender name=”ColoredConsoleAppender” type=”log4net.Appender.ColoredConsoleAppender”>
<mapping>
<level value =”ERROR”/>
<foreColor value =”Red”/>
</mapping>

<mapping>
<level value =”WARN”/>
<foreColor value =”Yellow”/>
</mapping>
<layout type=”log4net.Layout.PatternLayout”>
<ConversionPattern value=”%date %messge%newline”/>
</layout>
</appender>

<root>
<level value=”ALL” />
<appender-ref ref=”LogFileAppender” />
<appender-ref ref=”RollingFileAppender” />
<appender-ref ref=”ColoredConsoleAppender” />
</root>

<logger name=”FileLogger” additivity=”false”>
<level value=”ALL” />
<appender-ref ref=”LogFileAppender” />
</logger>

<logger name = “ConsoleLogger” additivity=”false”>
<level value=”ALL” />
<appender-ref ref=”ColoredConsoleAppender” />
</logger>
</log4net>

</configuration>

Working App.config

——–

<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>
<!– <log4net configSource=”log4net.config” /> –>
<configSections>
<section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler, log4net” />
</configSections>
<log4net>
<appender name=”RollingFileAppender” type=”log4net.Appender.RollingFileAppender”>
<File value=”File1.log” />
<AppendToFile value=”true” />
<rollingStyle value=”Composite” />
<maximumFileSize value=”200MB” />
<layout type=”log4net.Layout.PatternLayout”>
<ConversionPattern value=”%date %messge%newline”/>
</layout>
</appender>

<appender name=”LogFileAppender” type=”log4net.Appender.RollingFileAppender”>
<File value=”File2.log” />
<AppendToFile value=”true” />
<rollingStyle value=”Date” />
<maximumFileSize value=”100MB” />
<layout type=”log4net.Layout.PatternLayout”>
<ConversionPattern value=”%date %messge%newline”/>
</layout>

<filter type=”log4net.Filter.LevelRangeFilter”>
<acceptOnMatch value=”true” />
<levelMin value=”INFO” />
</filter>
<filter type=”log4net.Filter.LevelRangeFilter”>
<acceptOnMatch value=”true” />
<levelMin value=”FATAL” />
</filter>
</appender>

<appender name=”ColoredConsoleAppender” type=”log4net.Appender.ColoredConsoleAppender”>
<mapping>
<level value =”ERROR”/>
<foreColor value =”Red”/>
</mapping>

<mapping>
<level value =”WARN”/>
<foreColor value =”Yellow”/>
</mapping>
<layout type=”log4net.Layout.PatternLayout”>
<ConversionPattern value=”%date %messge%newline”/>
</layout>
</appender>

<root>
<level value=”ALL” />
<appender-ref ref=”LogFileAppender” />
<appender-ref ref=”RollingFileAppender” />
<appender-ref ref=”ColoredConsoleAppender” />
</root>

<logger name=”FileLogger” additivity=”false”>
<level value=”ALL” />
<appender-ref ref=”LogFileAppender” />
</logger>

<logger name = “ConsoleLogger” additivity=”false”>
<level value=”ALL” />
<appender-ref ref=”ColoredConsoleAppender” />
</logger>
</log4net>

<appSettings>
<add key=”AA” value=”log4net.config” />
</appSettings>

<startup>
<supportedRuntime version=”v4.0″ sku=”.NETFramework,Version=v4.0″/>
</startup>
</configuration>

C# code

string stra = ConfigurationManager.AppSettings[“AA”];

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: