4
votes

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?

5
One of the heroes of the novel "Atlas Shrugged" by Ayn Rand published in 1957.John Adams
And what is "who is john galt?"? It is the question people keep asking in Atlas Shrugged. And I suspect @SrinivasReddyThatiparthy already knew thatMiserable Variable

5 Answers

14
votes

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.

3
votes

For Windows apps, you can add this to your Program:Main() method:

log4net.Config.XmlConfigurator.Configure();
0
votes

Don´t use

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

try use single bars instead

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

Check your folder permissions and don´t forget to initialize the log4net on global.asax.cs using

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

Add the following in application start in global.asax.

log4net.Config.XmlConfigurator.Configure();

0
votes

In my case I was using a BufferedRollingFileAppender but I omitted its evaluator element:

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