I can't see what is wrong here. I just want to get log4net writing to a log file with my Outlook AddIn. I have the following in my 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 variations I have 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, it should write to a file called log-file.txt in my project/bin/Debug folder but I see nothing created. When I step into the code with the Debugger the methods of the log object appear to work without complaint. I also tried the following absolute specification for the file with the same lack of results:
<param name="File" value="c:\\try\\logger\\log-file.txt" />
Can someone spot my mistake?
Log4Net doesn't look in your app.config unless you tell him too. The log4net configuration you wrote in app.config could have also been written in a separate xml, or programatically in code.
You need to instruct log4net from where to take his configuration. See: http://logging.apache.org/log4net/release/manual/configuration.html
The easiest way to do it in your case is just add:
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
anywhere in your Properties\AssemblyInfo.cs file.
After you do this: replace "c:\try\logger\log-file.txt" with only "log-file.txt" and after you run the program, you should then see in your Debug folder.
For Windows apps, you can add this to your Program:Main() method:
log4net.Config.XmlConfigurator.Configure();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With