24

I wrote a test console application in C# using log4net:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using log4net;
using log4net.Config;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace Log4Net_Test
{
    class Program
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Program));

        static void Main(string[] args)
        {    
            log.Info("Entering application");    
            log.Debug("Debug message");    
            log.Info("Leaving application");
        }
    }
}

My App.config file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
  </appSettings>
  <log4net>
    <!-- A1 is set to be a ConsoleAppender -->
    <appender name="A1" type="log4net.Appender.FileAppender">
      <file value="logfile.txt" />
      <appendToFile value="false" />

      <!-- A1 uses PatternLayout -->
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
      </layout>
    </appender>

    <!-- Set root logger level to DEBUG and its only appender to A1 -->
    <root>
      <level value="DEBUG" />
      <appender-ref ref="A1" />
    </root>
  </log4net>
</configuration>

Executing the test program ends up in the following error message:

log4net:ERROR Exception while reading ConfigurationSettings. Check your .config
file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed t
o initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognize
d configuration section log4net. 

What is wrong with my configuration file?

UPDATE 1: The configSections´ part was missing, as pointed out in the accepted answer. But I also had to remove thestartupsection, otherwise the same error appeared. I do not know why thestartup` section is causing the problem, too. Perhaps someone more experienced can tell and write a comment.

Michael Eakins
  • 4,149
  • 3
  • 35
  • 54
John Threepwood
  • 15,593
  • 27
  • 93
  • 149

3 Answers3

35

You need to add log4net also in the section block

<configSections>

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

</configSections>
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
13

The startup section needed to be removed as well because log4net does not support Framework .NET4.5

You can see my similar answer here: https://stackoverflow.com/a/13236410/1783224

Or you can see the supported frameworks in the documentation: http://logging.apache.org/log4net/release/features.html

Community
  • 1
  • 1
Milen Kindekov
  • 1,903
  • 1
  • 23
  • 31
4

You should also remember that section block <configSections> must be under the <configuration> otherwise will occurred an error:

Exception while reading ConfigurationSettings. Check your .config file is well formed XML.

For example:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>   
  <!-- Log4net Logging Setup -->   
  <log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
      <file value="service.log" />
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <!--
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
      -->
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%date [%thread] %-5level %logger.%method - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="FATAL" />
      </filter>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="FileAppender" />
    </root>   
  </log4net>   
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
</configuration>
elcudro
  • 841
  • 1
  • 7
  • 13
  • 3
    Wow - this helped me! But there is even more: The configSections-node HAS TO BE THE FIRST node under the configuration-node or else one will receive that message you mentioned. – Igor May 01 '17 at 00:08
  • this helped me also. config sections is the first node. – Gauravsa Jun 04 '18 at 11:58
  • yaaa, this helped me too. – Zafar Mar 05 '19 at 23:55
  • The comment made me think a bit - I have always had a separate file for the log4net.config - and seeing the startup at the bottom - made me move the log4net section into the app.config (I had already added a configuration section) and after moving the log4net section - it finally started working (.Net 4.6.1 project target) - not sure what changed - but now it is logging and not throwing exceptions on load. – Mark W. Mitchell Sep 26 '20 at 15:44