WCF中的Log4Net无法正常工作 [英] Log4Net in WCF not working

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

问题描述

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

解决方案

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

我们的设置步骤非常简单.

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

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

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

  4. 将log4net.dll参考添加到您需要记录的所有项目中
  5. 将记录器声明为需要记录的类的私有静态成员:

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

  6. 在需要的地方登录:

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

本文几乎涵盖了它: 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天全站免登陆