Logger file not saved, no logger shown in console

947 Views Asked by At

I have followed the log4net tutorial very carefully but the logger failed to save as a txt file and failed to appear in the console. I am expecting the log file to be saved under C:\temp.

Here is 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="C:\temp\Logs.txt"/>
      <param name="AppendToFile" value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="3MB"/>
      <staticLogFileName value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline"/>
      </layout>
    </appender>
    <root>
      <level value="Debug"/>
      <appender-ref ref="LogFileAppender"/>
    </root>
  </log4net>
</configuration>

I also included

[assembly: log4net.Config.XmlConfigurator(Watch = true)] in the AssemblyInfo.cs.

I placed my logger in the LoggerView.xaml.cs class. (I am using MVVM design pattern.)

[Export]
[PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)]
public partial class LoggerView : Window
{
    private LoggerViewModel _viewModel;
    protected static readonly ILog log = LogManager.GetLogger(typeof(LoggerView));

    [ImportingConstructor]
    public LoggerView(LoggerViewModel viewModel)
    {
        _viewModel = viewModel;
        this.DataContext = _viewModel;
        InitializeComponent();
    }

    static void Main(string[] args)
    {
        log4net.Config.XmlConfigurator.Configure();
        log.Debug("Debug Statement");
        log.Info("Info");
        log.Error("Error Statement");
        log.Warn("Warning Statement");
        log.Fatal("Fatal Statement");
    }
}

Is there anything that I am missing or doing wrong?

Thank you.

2

There are 2 best solutions below

8
On BEST ANSWER

Try specifying the config file in your AssemblyInfo.cs, mine is like this:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

Add an XML file log4net.config to your project, use this to start with:

    <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
  <configSections>

  </configSections>
  <log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="1MB" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <acceptOnMatch value="true" />
        <levelMin value="INFO" />
        <levelMax value="FATAL" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="=========================================
                                  %nLEVEL:........%-5level
                                  %nTIMESTAMP:....%date 
                                  %nMessage:
                                  %n%m
                                  %nUsername: %username
                                  %n=========================================
                                  %n" />
      </layout>
    </appender>

    <root>
      <level value="all" />
      <appender-ref ref="RollingFileAppender" />
      <appender-ref ref="SmtpAppender" />
    </root>
  </log4net>
</configuration>

then put this in your app (mine was just a simple console app that only logged "Test".

class Program
{
    private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    static void Main(string[] args)
    {
        log.Error("Test");
        Console.ReadLine();

    }
}
5
On

Verify that log4net has the correct permissions to write to the folder Regarding logging to the console you need to add the appender in as well see this post for more details How to configure log4net to print to console in debug mode

This is part of a config file which has log4net working correctly:

<log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingFileAppender" />
      <appender-ref ref="SmtpAppender" />
      <appender-ref ref="AdoNetAppender" />
    </root>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\Logs\EntitlementPermissionManagerService.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="100KB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
</log4net>