由IIS 7.5托管的WCF中的log4net没有写日志文件 [英] log4net in WCF hosted by IIS 7.5 no write log file

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

问题描述

首先抱歉我的英语不好)
我已经阅读了很多关于log4net loggining的文章但不幸的是我的问题还没有解决...
我有一个通过log4net登录的问题由IIS托管的WCF服务。

First of all sorry for my bad english) I have read a lot of articles about log4net loggining but unfortanly my problem is not resolved yet... I have a problem with loggining via log4net in WCF services hosted by IIS.

重要代码的某些部分

我的服务代码部分:

using System;
    [assembly: log4net.Config.XmlConfigurator(Watch = true)]
    namespace Service
    {
        [ServiceBehavior]
        public class MyService : IService
        {
            private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

服务方式的一部分

public string Name()
        {
            log.Info("return name ");
            return "NAME";
        }

这是我为log4net服务的配置文件

this is my config file for service for log4net

?xml version="1.0"?>
<configuration>
  <!-- Register a section handler for the log4net section -->
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
  </configSections>
  <appSettings>
    <!-- To enable internal log4net logging specify the following appSettings key -->
    <!-- <add key="log4net.Internal.Debug" value="true"/> -->
  </appSettings>
  <!-- This section contains the log4net configuration settings -->
  <log4net>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <file value="log-file.txt"/>
      <appendToFile value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%ndc] ID=%property{EventID} - %message%newline" />
      </layout>
    </appender>
    <!-- Define some output appenders -->
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%ndc] ID=%property{EventID} - %message%newline" />
      </layout>
    </appender>
    <!-- Setup the root category, add the appenders and set the default level -->
    <root>
      <level value="ALL" />
      <appender-ref ref="LogFileAppender" />
      <appender-ref ref="ConsoleAppender" />
    </root>
  </log4net>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

crossdomain.xml
clientaccesspolicy.xml 
Service.svc

文件已配置并出现在目录
C:\inetpub \wwwroot
中,我的服务工作成功但日志不写...

files is configured and present in the directory C:\inetpub\wwwroot and my service is work succefuly but log is not writing...

问题:
1)哪里必须找到配置文件Service.config和log4net配置?
2)我的Service.dll lib和Service.config位于C:\inetpub \ www.root \ bin中。它正常吗?

question: 1) where must bee located config file Service.config with log4net configuration? 2) my Service.dll lib and Service.config is located in C:\inetpub\wwwroot\bin. Its normal?

请帮我解决这个问题。
我尝试了超过10个其他变量的loggining。所有这些都在WindowsForms应用程序中工作,但在IIS中托管的服务中不起作用。
补充信息:当我在服务中调用方法NAME时?我尝试通过File.Create在当前目录中写入文件并且它的工作,但是没有创建loggining文件,我不知道如何解决它。
感谢大家尝试理解我的英语并寻求帮助。)

Please help me to resolve this problem. I'm try more than 10 jther varriants of loggining. All of them is working in WindowsForms Application but not work in service hosted in IIS. Additional info: when I call method NAME in service? I try to write file in current directory via File.Create and it's work, but loggining file is not created and I dont know how to resolve it. Thank you all for trying of understanding my English and for help.)

或者你是否已经或可以使用log4net loggining创建原始的工作WCF服务通过IIS发布写在这个答案的代码示例中,用于我的环境测试。

or if you have or can create primitive working WCF service with log4net loggining that is can be published via IIS write in this answers sample of code for my testing on my environment.

您好。
谢谢你的回复。
现在我尝试详细描述我的情况以获取更多信息
第一步:我创建新项目。项目类型 - WCF服务库。
我的服务接口代码是

Hi. Thank you for ansver. Now I try discribe my situation in details for more info First step: I create new Project. Type of project - WCF service library. My interface code for service is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace MyService
{
    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        string ReturnName();
    }
}

我的服务等级是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyService
{
    public class MyService:IMyService
    {
        public string ReturnName()
        {
            return "Return name";
        }
    }
}

然后我从visual studio发布我的IIS
服务结果 - 网站目录中的新服务文件(http://img854.imageshack.us/img854/456/62137541.png)

Then I publish from visual studio my service to IIS as result - new files of service in site directory (http://img854.imageshack.us/img854/456/62137541.png)

web.config file for this service is
<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="MyService.MyService">
        <endpoint address="" binding="wsHttpBinding" contract="MyService.IMyService">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/MyService/Service1/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

现在我创建了用于检查服务的测试控制台应用程序。我使用System添加服务引用到控制台应用程序并编写代码

Now I create test console app for checking service. I add service reference to console app and write code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestWCFLog.ServiceReference1;

namespace TestWCFLog
{
    class Program
    {
        static void Main(string[] args)
        {
            MyServiceClient service = new MyServiceClient();
            Console.WriteLine( service.ReturnName());
            Console.ReadLine();
        }
    }
}

这是有效的。 (http://img836.imageshack.us/img836/1718/52151311.png)
现在请告诉我,我必须添加到我的服务代码和web.config文件中?
我如何为服务添加log4net登录?
我尝试了很多样品,但它不起作用。
谢谢你的回答。
谢谢。

And it's work. (http://img836.imageshack.us/img836/1718/52151311.png) Now tell me please what I must add to my service code and to web.config file? How I can add log4net loginning for service? I try a lot of samples but it doesnt work. Thank you for answer. Thank you.

推荐答案

您需要初始化log4net,因为您使用的是IIS,您可以在全局中执行此操作.asax文件(如果你还没有项目,请在项目中添加一个新文件。)

You need to initialise log4net and because you are using IIS you can do it in your Global.asax file(add a new one to your project if you dont have one already).

它应该是这样的:

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        XmlConfigurator.Configure();

        var logger = LogManager.GetLogger(typeof(Global));

        logger.Info("Starting up services");
    }
}

我还可以在配置文件中看到你是使用旧版IgnoreSectionHandler。
将您的配置文件更改为:

I can also see in your config file that you are using the legacy IgnoreSectionHandler. change your config file to read like:

<configSections>
    <section name="log4net"
      type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>

你的配置文件需要被称为web.config,它需要在你的服务主机文件夹下bin文件夹,在你的情况下c:\inetpub \ www.root。

your config file needs to be called web.config and it needs to be under your service host folder not bin folder, in your case c:\inetpub\wwwroot.

不确定你的意思是service.config。在IIS下托管服务时,所有配置都会进入web.config文件。

Not sure what you mean by service.config. When hosting services under IIS all the configuration goes into a web.config file.

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

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