WCF 中的 Log4Net 不起作用 [英] Log4Net in WCF not working

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

问题描述

您好,我正在尝试在 WCF IIS 托管服务中使用 Log4Net,但它不记录任何数据.有没有人使用 Log4Net 实现 WCF 服务的登录?

解决方案

我在自托管 WCF 应用程序中的项目中成功使用了 log4net.

我们的设置步骤相当简单.

  1. 向我们的控制台服务宿主项目(我们的应用程序入口点)添加对 log4net.dll 的引用
  2. 将以下行添加到上述项目的 AssemblyInfo.cs 文件(允许指定自定义 log4net 配置文件,log4net 将监视"更新.快速,但可能有点脏..)

    [程序集:log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

  3. 将 log4net.config 文件添加到控制台项目并复制到输出目录(文件属性:复制到输出目录")

  4. 向所有需​​要记录的项目添加 log4net.dll 引用
  5. 将记录器声明为需要记录的类的私有静态成员:

     私有静态只读 log4net.ILog Logger =log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

  6. 在需要的地方记录:

    Logger.Info("启动控制台服务主机");

这篇文章几乎涵盖了它:http://haacked.com/archive/2005/03/07/ConfiguringLog4NetForWebApplications.aspx

Hi I am trying to use Log4Net in WCF IIS hosted service, but it doesn't log any data. Has anyone achieved logging in WCF service using Log4Net?

解决方案

I'm successfully using log4net on my project within a self-hosted WCF application.

Our steps to setup are fairly straightforward.

  1. Add reference to log4net.dll to our console service host project (our application entry point)
  2. Add the following line to the above project's AssemblyInfo.cs file (allows a custom log4net config file to be specified, which log4net will "watch" for updates. Quick, but maybe a bit dirty..)

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

  3. Add log4net.config file to console project and copy it to the output directory (file properties: "Copy to Output Directory")

  4. Add log4net.dll reference to all projects where you require logging
  5. Declare the logger as private static member of the classes where you need logging:

     private static readonly log4net.ILog Logger = 
     log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    

  6. Log where required:

    Logger.Info("Starting console service host");

This article pretty much covers it: http://haacked.com/archive/2005/03/07/ConfiguringLog4NetForWebApplications.aspx

这篇关于WCF 中的 Log4Net 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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