Log4Net Setup in a WPF app

末鹿安然 提交于 2020-07-08 21:24:43

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/49875761/log4net-setup-in-a-wpf-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!