Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net will not read from app.config

I have two projects configured identically for log4net. One project logs fine; however, the other does not log at all.

The Logger in the project that is not logging returns IsFatalEnabled = false, IsErrorEnabled = false, IsWarnEnabled = false, IsInforEnabled = false and IsDebugEnabled = false.

I've copied and pasted from one project to the other, replaced the file completely and tried removing all whitespace.

What could be causing the one project not to properly be reading the correct levels from the app.config?

app.config:

<?xml version="1.0" encoding="utf-8"?> <configuration>   <configSections>     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />   </configSections>   <log4net>     <appender name="FileAppender" type="log4net.Appender.FileAppender">       <file value="logfile.txt" />       <appendToFile value="true" />       <layout type="log4net.Layout.PatternLayout">         <conversionPattern value="%date: %-5level – %message%newline" />       </layout>     </appender>     <root>       <level value="DEBUG" />       <appender-ref ref="FileAppender" />     </root>   </log4net> </configuration> 

Program.cs

using log4net;  class Program {     private static readonly ILog Log = LogManager.GetLogger("SO");      static void Main(string[] args)     {         Log.Info("SO starting");     } } 
like image 314
Even Mien Avatar asked Aug 25 '10 12:08

Even Mien


People also ask

Where do I put log4net in web config?

Add log4net in config file config and enter the following details. Add a class Log. cs in the Utilities folder. Now, in the constructor of this class, instantiate logs for monitoring and debugger loggers.

How do I initialize log4net?

Configuration Initialization ConfigureAndWatch to initialize log4net setup as per the config setup in your main entry method like main method of program class. Method “ ConfigureAndWatch ” configures log4net using the file specified, monitors the file for changes and reloads the configuration if a change is detected.


1 Answers

It seems that the app.config file was not configured to be watched by log4net.

I added the following line to AssemblyInfo.cs and logging is now enabled:

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

Weird, since I didn't add this line to the the project that was working.

EDIT:

Looks like the project that was working did have the line after all. I must have forgotten.

// This will cause log4net to look for a configuration file  // called [ThisApp].exe.config in the application base  // directory (i.e. the directory containing [ThisApp].exe)  // The config file will be watched for changes.  [assembly: log4net.Config.XmlConfigurator(Watch = true)] 
like image 179
Even Mien Avatar answered Nov 13 '22 03:11

Even Mien