WCF 中的 Log4Net 不工作

发布于 2024-10-10 03:03:48 字数 76 浏览 5 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

颜漓半夏 2024-10-17 03:03:48

我在自托管 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. 将记录器声明为需要日志记录的类的私有静态成员:

     私有静态只读 log4net.ILog 记录器 = 
     log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    
  6. 在需要的地方记录:

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

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

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

何其悲哀 2024-10-17 03:03:48

另一件需要注意的事情是,当您在 Wcf 端点的单独程序集中定义了自定义 WCF ServiceHostFactory 时(例如,为了保持 DRY),那么定义工厂的程序集就是“根”从 log4net 的角度来看,这是一个程序集,并且需要在该程序集中声明 XmlConfigurator 属性。

此常见问题解答 http://logging.apache.org/log4net /release/faq.html#trouble-webapp-stops-logging 和链接的注释解释了 log4net 希望尽快初始化

Another thing to be careful of is when you have a custom WCF ServiceHostFactory defined in a separate assembly to your Wcf endpoint (eg to keep DRY) then the assembly where the factory is defined is the the "root" assembly from the point of view of log4net and the XmlConfigurator attribute needs to be declared in that assembly.

This FAQ http://logging.apache.org/log4net/release/faq.html#trouble-webapp-stops-logging and linked comments explain that log4net wants to initialize as soon as possible

浅听莫相离 2024-10-17 03:03:48

使用程序集属性来配置 log4net 将不起作用,因为 log4net 不会检查配置文件的正确目录。也许您可以在指定配置文件时尝试使用一些相对路径,但使用建议的解决方案之一会更容易此处

Using the assembly attribute to configure log4net will not work since log4net will not check the right directory for the config file. Maybe you could try to use some relative path when specifying the config file but it is easier to use one of the suggested solutions here.

堇年纸鸢 2024-10-17 03:03:48

我在 IIS 7 中托管的 MSMQ 激活的 WCF 服务中使用 log4net。我使用 Ninject 进行依赖项注入并安装了 Ninject.Extensions.WcfNinject.Extensions.Logging.log4net NuGet包并将 log4net 配置部分添加到 Web.config 文件中。

我见过有人建议将 log4net 初始化代码添加到 Global.asax,但据我所知,这对于 MSMQ 激活的服务不起作用。但是,Ninject 安装WebActivator 包,允许您在 Web 应用程序启动时运行初始化代码。 Ninject 将其自己的代码放入 App_Start\NinjectWebCommon.cs 中。要配置 log4net,您需要向 Start 方法添加代码。我的看起来像这样:

public static void Start() 
{
    DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
    DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));

    // configure log4net
    XmlConfigurator.Configure();

    bootstrapper.Initialize(CreateKernel);
}

I am using log4net in MSMQ activated WCF service hosted in IIS 7. I use Ninject for dependency injection and installed Ninject.Extensions.Wcf, Ninject.Extensions.Logging.log4net NuGet packages and added log4net configuration section to Web.config file.

I've seen people suggest adding log4net initialization code to Global.asax, but this doesn't work for MSMQ activated services as far as I can tell. However, Ninject installs WebActivator package that allows you to run initialization code when web application starts. Ninject places its own code into App_Start\NinjectWebCommon.cs. To configure log4net you need to add code to Start method. Mine looks like this:

public static void Start() 
{
    DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
    DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));

    // configure log4net
    XmlConfigurator.Configure();

    bootstrapper.Initialize(CreateKernel);
}
孤云独去闲 2024-10-17 03:03:48

我遇到了与此相同的问题,这是因为我使用自定义配置文件的相对路径。我使用了以下代码行:

log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));

通过 log4nets 调试输出,它告诉我它正在 IIS 根目录中查找 log4net.config 文件,而不是在我的应用程序路径中的任何位置。

因此,将行更改为更直接的内容以避免此问题(如果您也以这种方式配置自定义配置):

log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(@"G:\MyProject\log4net.config"));

I had the same issue as this and it was because I was using the relative path for the custom config file. I had the following line of code used:

log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));

Through log4nets debug output, it was telling me it was looking for the log4net.config file in a root IIS directory, rather than anywhere in my application path.

So, change the line to something more direct to avoid this issue (if you also configure your custom config in this way):

log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(@"G:\MyProject\log4net.config"));
你没皮卡萌 2024-10-17 03:03:48

Iısusr 许可 好的。

授予写入和修改权限。

汇编.cs
在最后添加:

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

Iısusr permission Okay.

Give write and modify permission.

Assamble.cs
Add at the end:

[assembly: log4net.Config.XmlConfigurator(Watch = true)] 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文