Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is log4net not recognized in configuration file?

Tags:

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.

like image 907
John Threepwood Avatar asked Jun 14 '13 10:06

John Threepwood


People also ask

Where is log4net configuration file?

You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation.


1 Answers

You need to add log4net also in the section block

<configSections>  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>   </configSections> 
like image 183
Massimiliano Peluso Avatar answered Oct 02 '22 14:10

Massimiliano Peluso