首先抱歉我的英語不好) 我已經閱讀了很多關於log4net日誌記錄的文章,但不幸的是我的問題還沒有解決...... 我有一個問題通過log4net在由IIS託管的WCF服務中進行日誌記錄。在由IIS 7.5託管的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文件 和我的服務就是工作succefuly但日誌不寫......
問題: 1),其中必須位於蜜蜂配置文件Service.config與log4net配置? 2)我的Service.dll庫和Service.config位於C:\ inetpub \ wwwroot \ bin中。這是正常的?
請幫我解決這個問題。 我嘗試了超過10個日誌記錄的變種。他們都在WindowsForms應用程序中工作,但不能在IIS中託管的服務中工作。 附加信息:當我在服務中調用方法名稱?我嘗試通過File.Create在當前目錄中寫入文件,它是可行的,但是沒有創建日誌文件,我不知道如何解決它。 謝謝大家,想了解我的英文和尋求幫助。)
或者如果您有或可以創建原始的工作WCF服務與可以通過IIS發佈的log4net日誌記錄寫在這個答案我的測試代碼示例在我的環境中。
非常感謝。
嗨。 謝謝你的答覆。 現在我嘗試詳細描述我的情況瞭解更多信息 第一步:我創建新項目。項目類型 - 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登錄服務? 我嘗試了很多樣品,但它不起作用。 謝謝你的回答。 謝謝。
你也可以在你的AssemblyInfo.cs類中添加一行:'[assembly:log4net.Config.XmlConfigurator()]',無論你是否在IIS上都可以工作。 –