Log4net fileappender does not create log-file.txt file in Outlook ThisAddIn.cs

I do not see what is wrong here. I just want log4net to write to the log file using Outlook AddIn. I have the following app.config file:

<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/> </configSections> <log4net> <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" > <param name="File" value="log-file.txt" /> <param name="AppendToFile" value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="10MB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} โ€“ %m%n" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="LogFileAppender" /> </root> </log4net> </configuration> 

Here are the relevant statements in my startup class, ThisAddIn.cs (comments show the options I tried):

 //protected static readonly ILog log = LogManager.GetLogger("application-log"); public static readonly ILog log = LogManager.GetLogger(typeof(ThisAddIn)); private void ThisAddIn_Startup(object sender, System.EventArgs e) { //BasicConfigurator.Configure(); //XMLConfigurator.Configure(); log.Info("Application Start"); log.Warn("This is a warning message."); log.Debug("This is a debug message"); if (log.IsDebugEnabled) { log.Debug("This is another debug message"); } 

In my research of this, he should write a file called log-file.txt in my project / bin / Debug folder, but I canโ€™t see anything. When I enter the code using the debugger, the methods of the log object work without complaint. I also tried the following absolute specification for a file with the same lack of results:

 <param name="File" value="c:\\try\\logger\\log-file.txt" /> 

Can anyone spot my mistake?

+4
source share
5 answers

Log4Net does not look into your app.config unless you tell it about it. The log4net configuration you wrote in app.config could also be written in separate xml or programmatically in code.

You need to tell log4net where to get its configuration. See: http://logging.apache.org/log4net/release/manual/configuration.html

The easiest way to do this in your case is to simply add:

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

anywhere in your \ AssemblyInfo.cs properties file.

After that: replace "c: \ try \ logger \ log-file.txt" with only "log-file.txt", and after starting the program you should see it in your Debug folder.

+14
source

In Windows applications, you can add this to your Program: Main () method:

 log4net.Config.XmlConfigurator.Configure(); 
+4
source

Do not use

<param name="File" value="c:\try\logger\log-file.txt" />

try using single bars instead

<param name="File" value="c:\try\logger\log-file.txt" />

Check the permissions of the folder and remember to initialize log4net on global.asax.cs using

  protected void Application_Start() { log4net.Config.XmlConfigurator.Configure(); ... } 
0
source

Add the following to the top of the application in global.asax.

log4net.Config.XmlConfigurator.Configure();

0
source

In my case, I used BufferedRollingFileAppender , but I missed its evaluator element:

  <evaluator type="log4net.Core.LevelEvaluator"> <threshold value="DEBUG"/> </evaluator> 
0
source

All Articles