ASP.NET Core 2+ 中的 ILogger 和 DependencyInjection [英] ILogger and DependencyInjection in ASP.NET Core 2+

查看:28
本文介绍了ASP.NET Core 2+ 中的 ILogger 和 DependencyInjection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到 Startup.cs 中的 ConfigureServices 中没有显式的 ILogger 注册.

第一个问题:ILogger 如何被注入到例如控制器.

第二个问题:如何配置 ILogger 以注入中间件?

解决方案

日志记录作为 HostBuilder.Build 进程

private void CreateServiceProvider(){var services = new ServiceCollection();services.AddSingleton(_hostingEnvironment);services.AddSingleton(_hostBuilderContext);services.AddSingleton(_appConfiguration);services.AddSingleton();services.AddSingleton();services.AddSingleton();服务.AddOptions();services.AddLogging();//<--HERE//...

WebHostBuilder.BuildCommonServices

 private IServiceCollection BuildCommonServices(out AggregateException hostingStartupErrors){//... 为简洁起见删除了代码var services = new ServiceCollection();services.AddSingleton(_options);services.AddSingleton(_hostingEnvironment);services.AddSingleton(_hostingEnvironment);services.AddSingleton(_context);var builder = new ConfigurationBuilder().SetBasePath(_hostingEnvironment.ContentRootPath).AddConfiguration(_config);_configureAppConfigurationBuilder?.Invoke(_context, builder);var 配置 = builder.Build();services.AddSingleton(配置);_context.Configuration = 配置;var listener = new DiagnosticListener("Microsoft.AspNetCore");services.AddSingleton(监听器);services.AddSingleton(监听器);services.AddTransient();services.AddTransient();services.AddScoped();服务.AddOptions();services.AddLogging();

要将 ILogger 注入控制器,只需将其作为依赖项包含在构造函数中

私有只读 ILogger 记录器;公共我的控制器(ILogger logger){this.logger = 记录器;}//...

并且框架会在它被激活时将其注入控制器.

参考 依赖注入ASP.NET Core 中的控制器

对于中间件vai构造函数注入也可以做到,就像控制器一样,

或直接进入每个请求的依赖

public Task Invoke(HttpContext context, ILogger logger) {//...}

像任何其他注入的服务

参考 ASP.NET Core 中间件

I notice there is no explicit ILogger registration in ConfigureServices in Startup.cs.

First question: how does ILogger get injected into e.g. controllers.

Second question: how do I configure ILogger to get injected into middleware?

解决方案

Logging is added as part of the HostBuilder.Build process

private void CreateServiceProvider()
{
    var services = new ServiceCollection();
    services.AddSingleton(_hostingEnvironment);
    services.AddSingleton(_hostBuilderContext);
    services.AddSingleton(_appConfiguration);
    services.AddSingleton<IApplicationLifetime, ApplicationLifetime>();
    services.AddSingleton<IHostLifetime, ConsoleLifetime>();
    services.AddSingleton<IHost, Host>();
    services.AddOptions();
    services.AddLogging();//<--HERE

    //...

WebHostBuilder.BuildCommonServices

    private IServiceCollection BuildCommonServices(out AggregateException hostingStartupErrors)
    {

        //... code removed for brevity

        var services = new ServiceCollection();
        services.AddSingleton(_options);
        services.AddSingleton<IHostingEnvironment>(_hostingEnvironment);
        services.AddSingleton<Extensions.Hosting.IHostingEnvironment>(_hostingEnvironment);
        services.AddSingleton(_context);

        var builder = new ConfigurationBuilder()
            .SetBasePath(_hostingEnvironment.ContentRootPath)
            .AddConfiguration(_config);

        _configureAppConfigurationBuilder?.Invoke(_context, builder);

        var configuration = builder.Build();
        services.AddSingleton<IConfiguration>(configuration);
        _context.Configuration = configuration;

        var listener = new DiagnosticListener("Microsoft.AspNetCore");
        services.AddSingleton<DiagnosticListener>(listener);
        services.AddSingleton<DiagnosticSource>(listener);

        services.AddTransient<IApplicationBuilderFactory, ApplicationBuilderFactory>();
        services.AddTransient<IHttpContextFactory, HttpContextFactory>();
        services.AddScoped<IMiddlewareFactory, MiddlewareFactory>();
        services.AddOptions();
        services.AddLogging();

To get ILogger injected into a controller just include it in the constructor as a dependency

private readonly ILogger logger;

public MyController(ILogger<MyController> logger) {
    this.logger = logger;
}

//...

and the framework will inject it into the controller when it is being activated.

Reference Dependency injection into controllers in ASP.NET Core

The same can be done for Middleware vai constructor injection just like with the controller,

or directly into the Invoke method for per-request dependencies

public Task Invoke(HttpContext context, ILogger<MyMiddleware> logger) {
    //...
}

like any other injected service

Reference ASP.NET Core Middleware

这篇关于ASP.NET Core 2+ 中的 ILogger 和 DependencyInjection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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