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:
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:
="1.0"="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"):
= "1.0"= "utf-8"
<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):
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
<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?