1

I am trying to set up logging using log4net, with no prior experience of log4net, in a wpf application

taken from this SO answer, I have

public partial class App : Application
{
    private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    protected override void OnStartup(StartupEventArgs e)
    {
        XmlConfigurator.Configure();
        base.OnStartup(e);

        //Set data directory
        string baseDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\BlowTrial";
        if (!Directory.Exists(baseDir))
        {
            Directory.CreateDirectory(baseDir);
        }

        //Set logging
        foreach (var appender in LogManager.GetRepository().GetAppenders())
        {
            var fileAppender = appender as FileAppender;
            if (fileAppender != null)
            {
                fileAppender.File = fileAppender.File.Replace("|DataDirectory|", baseDir);
                     ...

but there are no elements in LogManager.GetRepository().GetAppenders() despite the app.config having the following within the configuration node:

<configSections>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
  <level value="DEBUG"/>
  <appender-ref ref="LogFileAppender"/>
  <appender-ref ref="ColoredConsoleAppender"/>
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
  <param name="File" value="|DataDirectory|\log.txt"/>
  <param name="AppendToFile" value="true"/>
  <rollingStyle value="Size"/>
  <maxSizeRollBackups value="10"/>
  <maximumFileSize value="1MB"/>
  <staticLogFileName value="false"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger: %message%newline"/>
  </layout>
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger: %message%newline"/>
  </layout>
</appender>
</log4net>

I was wondering why the appender "LogFileAppender" is not an element within the LogManager.GetRepository().GetAppenders() method, and how I might change the name of the file?

Thanks for your expertise

Community
  • 1
  • 1
Brent
  • 4,611
  • 4
  • 38
  • 55

1 Answers1

2

The RollingFileAppender does some stuff internally when it's initialized (using XmlConfigurator.Configure();), which results in the path of |DataDirectory|\log.txt throwing a System.ArgumentException when trying to retrieve the path. You can view how to debug log4net if you want more details here.

According to this answer, since you're using a special folder path anyways, you can specify this in the app.config, and not worry about setting the path from the code:

<param name="File" type="log4net.Util.PatternString" value="%envFolderPath{CommonApplicationData}\\BlowTrial\\log.txt"/>
Community
  • 1
  • 1
matth
  • 6,112
  • 4
  • 37
  • 43