Log4Net不适用于控制台应用程序 [英] Log4Net not working for console app

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

问题描述

我有一个控制台应用程序,我正在尝试为其实现log4Net.

I have a console app and I am trying to implement log4Net for it.

我执行了以下步骤-

  1. 添加了log4Net参考
  2. 创建的Log4Net.config-

  1. added log4Net reference
  2. Created Log4Net.config -

  1. 创建了LogHelper.cs类-

  1. Created the LogHelper.cs class -

将以下内容添加到AssemblyInfo.cs中 [程序集:log4net.Config.XmlConfigurator(ConfigFile ="Log4net.config",Watch = true)]

Added the following to the AssemblyInfo.cs [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4net.config", Watch = true)]

但是日志记录似乎不起作用?有人可以建议在控制台应用程序中进行日志记录需要做什么吗?

But the logging doesnt seem to work? Can someone suggest what needs to be done for the logging to work in th console app?

推荐答案

  1. 确保将配置文件设置为复制到输出目录.

  1. Make sure the config file is set to copy to the output directory.

组件属性的log4net文档中说:

"..如果使用配置属性,则必须调用log4net以 允许它读取属性.一个简单的LogManager.GetLogger调用 将导致读取调用程序集上的属性,并 处理. 因此,必须以 在应用程序启动期间尽可能早地进行,当然 在加载和调用任何外部程序集之前."

".. if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked."

  1. 如果仍然无法正常运行,请像此答案
  2. 中那样启用log4net调试.
  1. If it still doesn't work, enable log4net debugging as in this answer

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

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