【问题标题】:ILogger and DependencyInjection in ASP.NET Core 2+ASP.NET Core 2+ 中的 ILogger 和 DependencyInjection
【发布时间】:2019-02-07 00:41:02
【问题描述】:

我注意到ConfigureServices 中的Startup.cs 中没有明确的ILogger 注册。

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

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

【问题讨论】:

    标签: c# asp.net-core logging dependency-injection .net-core


    【解决方案1】:

    日志记录作为HostBuilder.Build 进程的一部分添加

    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();
    

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

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

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

    参考Dependency injection into controllers in ASP.NET Core

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

    或直接进入Invoke方法为per-request dependencies

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

    像任何其他注入服务一样

    参考ASP.NET Core Middleware

    【讨论】:

    • 写得非常好。谢谢。似乎每个请求的依赖关系是有限的。例如,我无法将 ILogger 注入 Invoke。您知道每个请求依赖项的详细信息记录在哪里吗?
    猜你喜欢
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 2021-12-17
    • 2018-03-13
    • 2020-08-08
    • 1970-01-01
    相关资源
    最近更新 更多