4

I have an application (a console application that is self-hosting the ASP.Net WebAPI) that is calling XmlConfigurator.Configure() as part of its setup.

It works fine if my application's app.config file looks like this:

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

    <log4net>
    </log4net>
</configuration>

It hangs if I take out the empty <log4net> element:

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

I haven't seen this behaviour anywhere else when using log4net in the past (it usually just puts the message "log4net:ERROR XmlConfigurator: Failed to find configuration section 'log4net' in the application's .config file." out to the console).

Am I doing anything wrong, or have I stumbled across a bug in log4net?


I have debugged this extensively. There are two behaviours:

  1. the call to XmlConfigurator.Configure() hangs indefinitely when I step over it
  2. the call to XmlConfigurator.Configure() can be stepped over, but then the application seems to hang. If I pause execution and look at the active threads, there is one stuck in a log4net method (something like ConfigureFromFile).

Either way, it hangs the whole application.

Here is the stacktrace from the hung thread (in the second case above):

stack trace

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
  • what happends if you step through the code? – default Sep 28 '12 at 13:02
  • It might not be a bug in log4net, it could be the MS config parser. – Justin Harvey Sep 28 '12 at 13:03
  • You agree it's an error. In this case it might pop up in some static constructor, impossible for you to catch. – H H Sep 28 '12 at 13:04
  • It appears that you have the log4net config settings setup wrong..I will post a sample of how my working version of log4net is working for me currently on my enterprise web application – MethodMan Sep 28 '12 at 13:05
  • Hangs are easy to diagnose. Debug + Break All and looks at the call stack. Post it in your question if that doesn't help. NLog is the better port btw. – Hans Passant Sep 28 '12 at 13:17
  • @HansPassant: The call to configure log4net is on the main thread of the application, hence the whole thing hangs. (I am using NLog as well, but this integrates with an old legacy bit of code that uses log4net extensively). – adrianbanks Sep 28 '12 at 13:27

3 Answers3

4

I got to the bottom of this in the end thanks to this blog post.

It basically boils down to the fact that I have .Net 4.5 installed on my machine. Even though I am targeting .Net 4.0, the behaviour is different due to the nature of the drop-in-replacement nature of .Net 4.5.

Without the empty <log4net> element in my configuration file, log4net wants to write to standard error to notify that the element is empty. Without the element, no logging occurs. When the logging does occur (which happens on a different thread to other Console use in the application), it hits a deadlock caused by a change in how the Console is initialised in .Net 4.5 (detailed in the linked blog post).

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
  • Can you explain your accepted answer a little bit more please? What have you done to solve it? – MikroDel Oct 31 '13 at 13:10
  • 1
    @MikroDel: I ensured that there was logging configuration for my application. That way, there wasn't an empty `` element, so there was no attempt to log to the console, which was the cause of the deadlock. – adrianbanks Nov 01 '13 at 09:20
  • Thank you for your response. I have almost the same question [question](http://stackoverflow.com/questions/17468841/log4net-configuration-failed-to-find-section) where I have **web.config** and 3 configruations for debug, test and release. The one is empty. Can it be the problem? Anyway if you have an idea - post it as an answer in my question please. Ill vote it up and if it solve my error message - accept it – MikroDel Nov 01 '13 at 10:59
0

Your config tells to the XmlConfigurator manager there is a section called Log4Net. So your XmlConfigurator tries to load that section and complains.
I will dare to say that this is to be expected.
If you remove ANY section declared in the configSections you will get the same behavior with any derived Configuration class.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • I have had other applications in the past that do not demonstrate this behaviour, including ones that configure log4net but don't have a configuration section defined. Defining a handler for a config section does not mean that the section has to exist. – adrianbanks Sep 28 '12 at 13:12
0

try something like this , I have in my .config file after the settings

in 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>
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • 1
    I can make it work with configuration, that is not the problem. I am more interested in *why* it is behaving differently from every other application I have ever used with log4net in the past. – adrianbanks Sep 28 '12 at 13:23
  • I apologize I may have not understood your question initially. – MethodMan Sep 28 '12 at 13:26