WPF应用程序中的Log4Net设置 [英] Log4Net Setup in a WPF app

查看:81
本文介绍了WPF应用程序中的Log4Net设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不敢相信我要问这个,但是这里...

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

我正在尝试在新的WPF应用中设置log4net,由于某种原因,它没有创建日志文件并记录任何内容,所以这是我到目前为止已完成的步骤...

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 ...

从nuget添加最新版本(v2.0.8.0)参考之后.

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

在AssemblyInfo.cs中:

In AssemblyInfo.cs:

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

在app.config中:

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" />

在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>

在App.xaml.cs中:

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 based on comments, I thought I should add this:

我已经看到了问题&这样的答案...使用 log4net 写入不同的记录器

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

...如果我将这样的内容添加到Log4Net.config中...

... 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.

推荐答案

如果您在DEV中,则需要将Log4Net.config复制到bin \ debug中;做与否?必须将复制设置为属性,或将其设置为资源文件,以便稍后包含在安装程序安装程序中.再见;感谢您的投票.

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.

这篇关于WPF应用程序中的Log4Net设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆