将 ILogger 或 ILoggerFactory 传递给 AspNet Core 中的构造函数? [英] Pass ILogger or ILoggerFactory to constructors in AspNet Core?

查看:33
本文介绍了将 ILogger 或 ILoggerFactory 传递给 AspNet Core 中的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MS 文档文章 "介绍to Logging in ASP.NET Core"给出了2个构造函数注入的例子

The MS docs article "Introduction to Logging in ASP.NET Core" gives 2 examples of constructor injection

  • 使用ILogger

private readonly ILogger _logger;   
public TodoController(ILogger<TodoController> logger)  
{ 
    _logger = logger;   
}

  • ILoggerFactory

    private readonly ILogger _logger;  
    public TodoController( ILoggerFactory loggerFactory)  
    {  
        _logger = loggerFactory.CreateLogger<TodoController>();  
    }
    

  • 我的问题是我应该将什么传递给从我的控制器调用的

    My question is what should I pass to child classes called from my controller

    • ILoggerFactory 传递给我从控制器调用的子类,然后在每个班级电话中LoggerFactoryExtensions.CreateLogger()

    • pass ILoggerFactory to my child classes called from the controller and in each class call LoggerFactoryExtensions.CreateLogger<MyChildClass>() or

    将父控制器的ILogger 传递给每个子类从控制器创建并具有非通用参数 ILogger.

    pass parent controller's ILogger<MyController> to each child class created from the controller and having non-generic parameter ILogger.

    在日志中,我更喜欢看到每个类的单独类别MyChildClass",而不是所有类都使用来自父控制器的类别MyController".
    然而,每个对象构造中的 CreateLogger 可能是一项昂贵的操作(例如,请参阅 https://github.com/aspnet/Logging/issues/524)

    In logs I prefer to see separate category 'MyChildClass' for each class, rather than all classes use the category 'MyController' from the parent controller.
    However CreateLogger in each object construction can be an expensive operation (e.g. see https://github.com/aspnet/Logging/issues/524)

    您会推荐哪个选项?你能提出任何其他方法吗?

    Which option will you recommend? Can you suggest any other approach?

    推荐答案

    这更多是一个设计问题.

    This is more of a design issue.

    无论如何,控制器都不应该创建子类.这不是控制器应该处理的问题,并且违反了 SRP(单一职责原则).

    The controller should not be creating child classes anyway. That is not a concern the controller should be dealing with and goes against SRP (Single Responsibility Principle).

    我的问题是我应该将什么传递给从我的控制器调用的子类

    My question is what should I pass to child classes called from my controller

    如果您更喜欢将记录器分开,那么除了让子(依赖)类拥有自己的记录器之外,这里真的没有其他选择.

    If your preference is to separate the loggers, then there really isn't any other choice here than to have child (dependent) classes have their own loggers.

    让子类注入自己的记录器

    Have child classes inject their own logger

    public class TodoRepository : ITodoRepository {
        private readonly ILogger logger; 
    
        public TodoRepository(ILogger<TodoRepository> logger) { 
            this.logger = logger;   
        }
      
        //...
    }
    

    然后将子类注入控制器.

    and then inject the child class into the controller.

    public class TodoController : Controller {
        private readonly ILogger logger;
        private readonly ITodoRepository todoRepository;
    
        public TodoController(ILogger<TodoController> logger, ITodoRepository todoRepository) {
            this.logger = logger;   
            this.todoRepository = todoRepository;
        }
    
        //...
    }
    

    这样,当子类被解析并注入控制器时,子记录器将被解析.

    That way, the child loggers will get resolved when the child classes are being resolved and injected into the controller.

    这篇关于将 ILogger 或 ILoggerFactory 传递给 AspNet Core 中的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆