Log4Net in WCF not working

Hi, I am trying to use Log4Net in a hosted WCF IIS service, but it is not logging any data. Has anyone reached logging in to the WCF service using Log4Net?

+4
source share
4 answers

I successfully use log4net in my project in a self-contained WCF application.

Our setup steps are pretty simple.

  • Add a link to log4net.dll to our console host project (our entry point to the application)
  • Add the following line to the aforementioned AssemblyInfo.cs file (allows you to specify a custom log4net configuration file that log4net will β€œmonitor” for updates. Quick, but maybe a little dirty ..)

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

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

  • Add the log4net.dll link to all projects where logging is required
  • Declare the registrar as a private static member of classes in which you need to keep a log:

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

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

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

+7
source

Another thing to keep in mind is when you have a custom WCF ServiceHostFactory defined in a separate assembly to your Wcf endpoint (for example, to save DRY), then the assembly where the factory is defined is the "root" assembly with log4net points of view and the XmlConfigurator attribute must be declared in this assembly.

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

+1
source

Using the build attribute to configure log4net will not work, because log4net does not check the correct directory for the configuration file. Perhaps you can try to use some relative path when specifying the configuration file, but it is easier to use one of the suggested solutions here .

0
source

I use log4net in the included WCF service with WCM hosted in IIS 7. I use Ninject to inject dependencies and install Ninject.Extensions.Wcf , Ninject.Extensions.Logging.log4net NuGet and the log4net configuration section is added to the Web.config file.

I have seen people suggest adding log4net initialization code to Global.asax, but this does not work for activated MSMQ services, as far as I can tell. However, Ninject installs the WebActivator package, which allows you to run the initialization code when the web application starts. Ninject puts its own code in App_Start \ NinjectWebCommon.cs . To configure log4net, you need to add code to the Start method. My looks like this:

 public static void Start() { DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); // configure log4net XmlConfigurator.Configure(); bootstrapper.Initialize(CreateKernel); } 
0
source

Source: https://habr.com/ru/post/1334319/


All Articles