Log4Net in WCF not working

爱⌒轻易说出口 提交于 2019-12-17 14:00:56

问题


Hi I am trying to use Log4Net in WCF IIS hosted service, but it doesn't log any data. Has anyone achieved logging in WCF service using Log4Net?


回答1:


I'm successfully using log4net on my project within a self-hosted WCF application.

Our steps to setup are fairly straightforward.

  1. Add reference to log4net.dll to our console service host project (our application entry point)
  2. Add the following line to the above project's AssemblyInfo.cs file (allows a custom log4net config file to be specified, which log4net will "watch" for updates. Quick, but maybe a bit dirty..)

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

  3. Add log4net.config file to console project and copy it to the output directory (file properties: "Copy to Output Directory")

  4. Add log4net.dll reference to all projects where you require logging
  5. Declare the logger as private static member of the classes where you need logging:

     private static readonly log4net.ILog Logger = 
     log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    
  6. Log where required:

    Logger.Info("Starting console service host");

This article pretty much covers it: http://haacked.com/archive/2005/03/07/ConfiguringLog4NetForWebApplications.aspx




回答2:


Another thing to be careful of is when you have a custom WCF ServiceHostFactory defined in a separate assembly to your Wcf endpoint (eg to keep DRY) then the assembly where the factory is defined is the the "root" assembly from the point of view of log4net and the XmlConfigurator attribute needs to be declared in that assembly.

This FAQ http://logging.apache.org/log4net/release/faq.html#trouble-webapp-stops-logging and linked comments explain that log4net wants to initialize as soon as possible




回答3:


Using the assembly attribute to configure log4net will not work since log4net will not check the right directory for the config file. Maybe you could try to use some relative path when specifying the config file but it is easier to use one of the suggested solutions here.




回答4:


I am using log4net in MSMQ activated WCF service hosted in IIS 7. I use Ninject for dependency injection and installed Ninject.Extensions.Wcf, Ninject.Extensions.Logging.log4net NuGet packages and added log4net configuration section to Web.config file.

I've seen people suggest adding log4net initialization code to Global.asax, but this doesn't work for MSMQ activated services as far as I can tell. However, Ninject installs WebActivator package that allows you to run initialization code when web application starts. Ninject places its own code into App_Start\NinjectWebCommon.cs. To configure log4net you need to add code to Start method. Mine looks like this:

public static void Start() 
{
    DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
    DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));

    // configure log4net
    XmlConfigurator.Configure();

    bootstrapper.Initialize(CreateKernel);
}


来源:https://stackoverflow.com/questions/4602126/log4net-in-wcf-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!