Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been having this issue for some time now. When I start my console app, log4net keeps throwing the same error several times. I have tried numerous ways to get around the issue but it always occurs.

The code:
C#
public class Announcer
{
    private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    static void Main()
    {
        _ = XmlConfigurator.Configure(LogManager.GetRepository(Assembly.GetEntryAssembly()), new FileInfo("log4net.config"));
        _ = Log4NetUtility.SetLoggingLevel(DefaultLoggingLevel, LogManager.GetRepository());
    ...
    }
}

The app config file:
XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
  </startup>
  <appSettings>
    <add key="log4net.Internal.Debug" value="false" />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

The config file ("log4net.config"):
XML
<?xml version = "1.0" encoding = "utf-8" ?>
<!-- Log4net Logging Setup -->
  <log4net update="Overwrite">
    <appender name="console" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value="ERROR" />
        <foreColor value="White" />
        <backColor value="Red" />
      </mapping>
      <mapping>
        <level value="INFO" />
        <foreColor value="Green" />
      </mapping>
      <mapping>
        <level value="DEBUG" />
        <backColor value="Cyan" />
      </mapping>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5level %-35logger{2} [%thread] %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
      </filter>
    </appender>
    <appender name="logfile" type="log4net.Appender.RollingFileAppender">
      <file value="TestFiles\NewsAnnouncer.log" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyyMMdd" />
      <maxSizeRollBackups value="5" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %-35logger{2} [%thread] %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
      </filter>
    </appender>
    <appender name="DebugAppender" type="log4net.Appender.DebugAppender">
      <immediateFlush value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5level %-35logger{2} [%thread] %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="INFO" />
      <appender-ref ref="console" />
      <appender-ref ref="logfile" />
    </root>
  </log4net>

The error (occurs as soon as the call to LogManager.GetRepository() is made):
Terminal
log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize
 ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section system.web. (C:\Programming\NewsAnnouncer-System\NewsAnnouncer\NewsAnnouncer\bin\Debug\net6.0\NewsAnnouncer.dll.config line 10)
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationManager.PrepareConfigSystem()
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.get_AppSettings()
   at log4net.Util.SystemInfo.GetAppSetting(String key)
Note: NewsAnnouncer is the application's assembly name, hence NewsAnnouncer.dll.config.
The odd thing is that log4net ends up properly using my log4net.config file. My logging messages are going where I want and are formatted as I want. But I keep getting that error message repeated 5 times every time the app starts.

What I have tried:

I have tried with and without
XML
<configSections>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
as the first child of the <configuration> element in the app config.
I have looked at log4net's documentation, tried their examples, and searched for the problem on the net.
Most answers involve using the [assembly:...] attribute (which I tried to no avail), or including the log4net configuration directly in the app.config file. But these are not avenues that I wish to pursue: ultimately, I would like the location of the logfile to be configurable, which is why I chose this format that allows for setting the file location at runtime.
Does anyone see why I am getting the reported error?
Posted
Updated 24-Apr-23 4:39am

1 solution

Quote:
Unrecognized configuration section system.web.
The problem is that you have a <system.web> section in your app config file.

That section was used to configure ASP.NET in .NET Framework. It does not apply to .NET 6 applications, nor to console applications.

Remove that unsupported section to get rid of the error.

NB: You may also need to remove the <runtime> section, since that also doesn't apply to .NET 6 applications.
 
Share this answer
 
v2
Comments
C Pottinger 24-Apr-23 13:00pm    
Thank you, Richard.

Sometimes I can't see the forest for the trees. I didn't even occur to me that the error was not a log4net error, but an actual error in the config. I also didn't know that the <system.web> and <runtime> sections were no longer needed. I guess they just came along for the ride when I upgraded to .NET 6.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900