问题
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.
some parts of important code
my service code parts:
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);
a part of service method
public string Name()
{
log.Info("return name ");
return "NAME";
}
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
files is configured and present in the directory C:\inetpub\wwwroot and my service is work succefuly but log is not writing...
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?
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.)
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.
A lot of thanks.
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();
}
}
My class for service is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyService
{
public class MyService:IMyService
{
public string ReturnName()
{
return "Return name";
}
}
}
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>
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();
}
}
}
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.
回答1:
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).
It should read something like this:
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");
}
}
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>
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.
Not sure what you mean by service.config. When hosting services under IIS all the configuration goes into a web.config file.
来源:https://stackoverflow.com/questions/7854656/log4net-in-wcf-hosted-by-iis-7-5-no-write-log-file