IIS中托管的WCF服务库的log4net [英] log4net for WCF Service library hosted in IIS

查看:140
本文介绍了IIS中托管的WCF服务库的log4net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个项目,我有一个WCF服务库(目前非常简单)
,它通过WCF服务网站项目在IIS 7.5中托管。

For a project I have a WCF Service library (very simple at the moment) which is hosted in IIS 7.5 via a WCF Service Website project.

对于该WCF服务库,我需要log4net来记录一些重大事件。

For that WCF Service library I need log4net to log some major events.

但是在启动和访问网站后,没有创建日志文件。

But after starting and accessing the website, no logfile is created.

以下是我的配置详情:

WCF服务库:

App.config (is containing the following)

<appSettings>
    <add key="log4net-config-file" value="log4net.config"/>
    <!-- <add key="log4net.Internal.Debug" value="true"/> -->
</appSettings>

log4net.config

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
  </configSections>
  <log4net>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\\Temp\\Logfile.txt"/>
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <datePattern value="yyyyMMdd"/>
      <maxSizeRollBackups value="5"/>
      <maximumFileSize value="100KB"/>
      <staticLogFileName value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d [%t] %-5p %c - %m%n"/>
      </layout>
    </appender>
  </log4net>
</configuration>

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

On top of the Service class
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

In the Static Constructor (mainly due to fiddling around)
string configFile = ConfigurationManager.AppSettings["log4net-config-file"];
XmlConfigurator.Configure(new FileInfo(configFile));
logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

In the non-static constructor:
logger.Info("Hello world!");

Wcf服务网站项目:

 Web.Config is basically the same as App.Config of service library

 <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>

到目前为止我尝试了什么:

激活log4net内部日志记录(通常在DebugView中看到)

activated log4net internal logging (which is normally seen in DebugView)

DebugView中没有输出,IIS配置日志中没有关于log4net的信息。

There is no output in DebugView and nothing about log4net in the IIS configured logs.

我怎样才能找出问题所在?

How can I find out what is wrong?

我该怎么做才能找出它无效的原因?

What can I do to find out why it is not working?

推荐答案

问题是:

在IIS中托管时,在IIS的当前目录中搜索了log4net配置文件,这在我的情况下是:

When hosted in IIS, the log4net config file was searched in the current directory of IIS, which is in my case:

C:\Windows\System32\inetsrv

但配置文件保存在WCF服务的物理路径中。

But the config file is saved in the physical path of the WCF service.

所以我在 Web.config

<appSettings>
    <add key="log4net_config" value="log4net.config" />
</appSettings>

在服务类的构造函数中,我调用

And in the Constructor of the Service-class I call

var fullPath = ConfigurationManager.AppSettings["log4net_config"];

var physicalPath = System.Web.Hosting.HostingEnvironment.MapPath("/");

// to keep it compatible with self hosting 
if (physicalPath != null)
{
    fullPath = Path.Combine(physicalPath, fullPath);
}

var fileInfo = new FileInfo(configFile);

XmlConfigurator.ConfigureAndWatch(fileInfo);

这篇关于IIS中托管的WCF服务库的log4net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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