How to configure log4net for WCF

不想你离开。 提交于 2019-12-19 05:20:27

问题


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.


回答1:


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.




回答2:


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.




回答3:


Adding:

XmlConfigurator.Configure();

to the constructor of each of service classes did the trick for me.



来源:https://stackoverflow.com/questions/2446556/how-to-configure-log4net-for-wcf

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