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

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

问题描述

MS docs文章简介登录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>();  
    }
    

  • 我的问题是我应该如何传递给从控制器调用的 child

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

    • ILoggerFactory 传递给我的从控制器调用的子类, 在每堂课中 LoggerFactoryExtensions.CreateLogger<MyChildClass>()

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

    将父控制器的 ILogger<MyController> 传递给每个子类 由控制器创建并具有非通用参数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?

    推荐答案

    这更多是设计问题.

    无论如何,控制器都不应该创建子类.这不是控制器应该处理的问题,并且违反了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 it 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天全站免登陆