On my asp webforms app I would do the log4net initialization;
log4net.Config.XmlConfigurator.Configure();
on global.asax on Application_Start so that it is done once when the application starts.
What is the right way of configuring log4net for IIS hosted WCF apps(asking specifically about where to place this line which event to use etc) so that log4net gets initialized once on and there is no unnecessary initializations.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I usually do this in the constructor of my service class, but I check if log4net is already configured:
if (!LogManager.GetRepository().Configured)
{
// configure log4net...
}
I think if you really want to avoid this you would have to write your own service factory and perform the configuration there.
Method 2
Same thing: Application_Start. After all it is an ASP.NET application. For self hosting services you could configure log4net just before starting the host.
Method 3
Adding:
XmlConfigurator.Configure();
to the constructor of each of service classes did the trick for me.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0