Dependency injection and named loggers

前端 未结 5 1675
不思量自难忘°
不思量自难忘° 2020-12-02 05:22

I am interested in learning more about how people inject logging with dependency injection platforms. Although the links below and my examples refer to log4net and Unity, I

5条回答
  •  [愿得一人]
    2020-12-02 05:49

    I'm using Ninject to resolve the current class name for the logger instance like this:

    kernel.Bind().To()
      .WithConstructorArgument("currentClassName", x => x.Request.ParentContext.Request.Service.FullName);
    

    The constructor of a NLog Implementation could look like this:

    public NLogLogger(string currentClassName)
    {
      _logger = LogManager.GetLogger(currentClassName);
    }
    

    This approach should work with other IOC containers as well, I guess.

提交回复
热议问题