WCF 中的 Log4Net 不工作
您好,我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我在自托管 WCF 应用程序中的项目中成功使用了 log4net。
我们的设置步骤相当简单。
将以下行添加到上述项目的 AssemblyInfo.cs 文件(允许指定自定义 log4net 配置文件,log4net 将“监视”该文件)更新很快,但可能有点脏..)
[ assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
将 log4net.config 文件添加到控制台项目并将其复制到输出目录(文件属性:“复制到输出目录”)
将记录器声明为需要日志记录的类的私有静态成员:
在需要的地方记录:
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.
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)]
Add log4net.config file to console project and copy it to the output directory (file properties: "Copy to Output Directory")
Declare the logger as private static member of the classes where you need logging:
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 端点的单独程序集中定义了自定义 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 theXmlConfigurator
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
使用程序集属性来配置 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.
我在 IIS 7 中托管的 MSMQ 激活的 WCF 服务中使用 log4net。我使用 Ninject 进行依赖项注入并安装了 Ninject.Extensions.Wcf、Ninject.Extensions.Logging.log4net NuGet包并将 log4net 配置部分添加到 Web.config 文件中。
我见过有人建议将 log4net 初始化代码添加到 Global.asax,但据我所知,这对于 MSMQ 激活的服务不起作用。但是,Ninject 安装WebActivator 包,允许您在 Web 应用程序启动时运行初始化代码。 Ninject 将其自己的代码放入 App_Start\NinjectWebCommon.cs 中。要配置 log4net,您需要向 Start 方法添加代码。我的看起来像这样:
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:
我遇到了与此相同的问题,这是因为我使用自定义配置文件的相对路径。我使用了以下代码行:
通过 log4nets 调试输出,它告诉我它正在 IIS 根目录中查找 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:
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):
Iısusr 许可 好的。
授予写入和修改权限。
汇编.cs
在最后添加:
Iısusr permission Okay.
Give write and modify permission.
Assamble.cs
Add at the end: