WCF部署到IIS上之后log4net不记录日志的解决方案
问题及环境:在web项目中直接添加的wcf服务。web项目中增加Global.asax,在Global.asax的Application_Start事件中注册log4net
using System;using System.ServiceModel;using System.ServiceModel.Activation;namespace Test{ public class CustomServiceHostFactory : ServiceHostFactory { protected override ServiceHost CreateServiceHost( Type serviceType, Uri[] baseAddresses) { CustomServiceHost customServiceHost = new CustomServiceHost(serviceType, baseAddresses); return customServiceHost; } } public class CustomServiceHost : ServiceHost { public CustomServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses) { } protected override void ApplyConfiguration() { base.ApplyConfiguration(); } }}
在<%@ ServiceHost Language="C#" Debug="true" Service="Test.CustomService" CodeBehind=“Test.CustomService.svc.cs”>中加入
Factory=“Test.CustomServiceHostFactory”
即<%@ ServiceHost Language="C#" Debug="true" Service="Test.CustomService" Factory=“Test.CustomServiceHostFactory” CodeBehind=“Test.CustomService.svc.cs”>
再次部署后,第一次调用IIS上的WCF服务也可以自动启动记录日志了