Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4Net Setup in a WPF app

I can't believe i'm having to ask this but here goes ...

I'm trying to setup log4net in a new WPF app and for some reason it's not creating the log file and logging anything, so here's the steps I've done so far ...

After adding the latest version (v2.0.8.0) reference from nuget.

In AssemblyInfo.cs:

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

In app.config:

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a" />
  </configSections>
  <log4net configSource="Log4Net.config" />

In Log4Net.config:

<?xml version="1.0" encoding="utf-8"?>
<log4net>
  <appender name="rollingLogFile" type="log4net.Appender.RollingFileAppender">
    <file value="D:\Logs\WorkflowApp.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="100KB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-6level %logger - %message %exception%newline" />
    </layout>
  </appender>
  <root>
    <level value="DEBUG" />
    <appender-ref ref="rollingLogFile" />
  </root>
</log4net>

In App.xaml.cs:

ILog log = LogManager.GetLogger(typeof(App));

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    log.Debug("Initialising ...");
}

As one last final "try it and see if it works" I added this to the above method ...

XmlConfigurator.Configure();

... still no long file created.

So what did I miss?

EDIT:

So based on comments, I thought I should add this:

I have seen questions & answers like this ... Using log4net to write to different loggers

... If I add something like this to my Log4Net.config ...

  <logger name="File">
    <level value="DEBUG" />
    <appender-ref ref="rollingLogFile" />
  </logger>

Then change my logger construction to ...

ILog log = LogManager.GetLogger("File");

... this however still results in nothing happening, no log file but no exceptions and the code appears to run fine other than the logging calls which appear to execute but with no results.

like image 350
War Avatar asked Apr 17 '18 10:04

War


1 Answers

if you are in DEV, you need to copy the Log4Net.config into bin\debug ; done or not ? have to set copy to in propterties, or as resource file to be included in setup installer later. see you; thanks for your vote.

like image 100
GCamel Avatar answered Nov 19 '22 00:11

GCamel