log4net不登录Windows事件查看器 [英] log4net doesn't log in Windows Event Viewer

查看:74
本文介绍了log4net不登录Windows事件查看器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用log4net登录Windows事件查看器.
我创建了一个控制台应用程序(.NET Framework 4),添加了参考log4net.dll,在我的App.config中放置了以下代码:

I would like to log in the Windows Event Viewer using log4net.
I created a Console Application (.NET Framework 4), I added the reference log4net.dll, I put the following code in my App.config:

<configuration>
 <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
 </configSections>

<log4net>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
  </layout>
</appender>
<root>
  <level value="ALL"/>
  <appender-ref ref="EventLogAppender"/>
</root>
</log4net>

<startup><supportedRuntime version="v2.0.50727"/></startup>
</configuration>

然后我输入以下代码:

class Program
{
    static void Main(string[] args)
    {
        log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
        log.Error("test error", new Exception("error's exception", new Exception("error's innerexception")));

        Console.Read();
    }
}

它没有记录,什么也没发生,为什么?

It doesn't log, nothing happens, why?

谢谢

推荐答案

您需要调用更改:

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

收件人

[assembly: log4net.Config.XmlConfigurator(Watch = true)] 

当您指定ConfigFile = "App.config"时,它将查找App.config,但文件名将为[FileName].Config.

When you specify ConfigFile = "App.config" its going to look for App.config but your filename would be [FileName].Config.

这篇关于log4net不登录Windows事件查看器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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