Why does my application freeze if my log4net configuration section is missing in my configuration file?

I have an application (a console application that is a self-service ASP.Net WebAPI) that calls XmlConfigurator.Configure() as part of its configuration.

It works fine if the app.config application app.config looks like this:

 <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> </log4net> </configuration> 

It freezes if I take out an empty <log4net> element:

 <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> </configuration> 

I have not seen this behavior elsewhere when using log4net in the past (usually it just sends the message "log4net: ERROR XmlConfigurator: could not find the configuration section" log4net "in the application's .config file." Console).

Am I doing something wrong or am I stumbled upon an error in log4net?


I debugged this widely. There are two types of behavior:

  • XmlConfigurator.Configure() call hangs endlessly when I go through it
  • the call to XmlConfigurator.Configure() may be stepped over, but then the application seems to freeze. If I paused execution and looked at the active threads, then one got stuck in the log4net method (something like ConfigureFromFile ).

In any case, this freezes the entire application.

Here is the stack from the freeze thread (in the second case above):

stack trace

+4
source share
3 answers

I reached the end thanks to this blog post .

It basically comes down to the fact that I have .Net 4.5 installed on my machine. Although I am targeting .Net 4.0, the behavior is different due to the nature of the change in .Net 4.5 replacement.

Without an empty <log4net> element in my configuration file, log4net wants to write a standard error to notify that the element is empty. Without an element, registration does not occur. When logging occurs (which happens in another thread with a different use of the Console in the application), it comes to a standstill caused by a change in how the console is initialized in .Net 4.5 (described in detail in the related blog post).

+4
source

Your configuration tells the XmlConfigurator manager that there is a Log4Net section. So your XmlConfigurator is trying to load this section and complains.
I dare say that this can be expected.
If you delete ANY section declared in configuration sections, you will get the same behavior with any derived configuration class.

0
source

try something like this, in my .config file after Options

on your page declare something like this

 private static log4Net.ILog _logger = log4net.LogManager.GetLoggger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); <log4net> <root> <level value="ALL"/> <appender-ref ref="LogFileAppender"/> </root> <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender,log4net"> <param name="File value="c:\Logs\SampleLog.txt"/> <param name="AppendToFile value="true"/> <rollingStyle value="Size"/> <maximumFileSize value="2MB"/> <staticLogFileName value="false"/> <datePattern value="yyyyMMdd"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger -%message%newline"/> </layout> </appender> </log4net> 
0
source

Source: https://habr.com/ru/post/1436811/


All Articles