【问题标题】:log4net in WCF hosted by IIS 7.5 no write log fileIIS 7.5 托管的 WCF 中的 log4net 没有写入日志文件
【发布时间】:2011-10-21 20:00:45
【问题描述】:

首先对不起我的英语不好) 我已经阅读了很多关于 log4net 登录的文章,但不幸的是我的问题还没有解决...... 我在 IIS 托管的 WCF 服务中通过 log4net 登录时遇到问题。

部分重要代码

我的服务代码部分:

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 服务的配置文件

?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 我的服务工作成功,但日志没有写入......

问题: 1)必须将配置文件Service.config与log4net配置放在哪里? 2) 我的 Service.dll 库和 Service.config 位于 C:\inetpub\wwwroot\bin。正常吗?

请帮我解决这个问题。 我正在尝试超过 10 种不同的登录方式。它们都在 WindowsForms 应用程序中工作,但不在 IIS 中托管的服务中工作。 附加信息:当我在服务中调用方法 NAME 时?我尝试通过 File.Create 在当前目录中写入文件并且它可以工作,但是没有创建登录文件,我不知道如何解决它。 感谢大家尝试理解我的英语并提供帮助。)

或者,如果您拥有或可以使用可通过 IIS 发布的 log4net 登录创建原始工作 WCF 服务,请在此答案示例中编写代码,以便在我的环境中进行测试。

非常感谢。

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

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)

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>

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

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 登录? 我尝试了很多样品,但它不起作用。 谢谢你的答案。 谢谢。

【问题讨论】:

    标签: wcf iis log4net


    【解决方案1】:

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

    它应该是这样的:

    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。 将您的配置文件更改为:

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

    您的配置文件需要称为 web.config,它需要位于您的服务主机文件夹而不是 bin 文件夹下,在您的情况下为 c:\inetpub\wwwroot。

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

    【讨论】:

    • 您也可以在您的 AssemblyInfo.cs 类中添加一行:[assembly: log4net.Config.XmlConfigurator()],无论您是否在 IIS 上都可以使用。
    猜你喜欢
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    • 2023-03-31
    相关资源
    最近更新 更多