How to use log4net with Dependency Injection

前端 未结 4 1889
死守一世寂寞
死守一世寂寞 2020-12-04 09:16

I\'m trying to figure out what the right patter and usage of log4net is with a dependency injection framework.

Log4Net uses the ILog interface but requires me to ca

4条回答
  •  余生分开走
    2020-12-04 10:12

    Another way of doing it would be to wire up the log4net ILog interface container registration sequence like this: (using Castle in an ASP.NET context below as an example)

    container.Register(Component.For().LifeStyle
        .PerWebRequest.UsingFactoryMethod(() => LogManager.GetLogger(
        MethodBase.GetCurrentMethod().DeclaringType)));
    

    and just constructor-inject ILog whenever you need it.

提交回复
热议问题