简体   繁体   English

注入 ILogger<t> 进入只需要 ILogger 的构造函数</t>

[英]Inject ILogger<T> into constructor that only expects ILogger

This is similar to How to inject named logger generic ILogger<T> as ILogger into constructor using IServiceCollection and NLog这类似于How to injection named logger generic ILogger<T> as ILogger into constructor using IServiceCollection and NLog

However I am looking for a solution using Serilog + Autofac.但是我正在寻找使用 Serilog + Autofac 的解决方案。

Hopefully there is a solution that works for both ASP.net Core 6 and console .NET 6希望有一个适用于 ASP.net Core 6 和控制台 .NET 6 的解决方案

Typically MEL ILogger is used like this:通常,MEL ILogger 的使用方式如下:


public class MyClass
{
  private readonly ILogger _logger;

  public MyClass(ILogger<MyClass> logger) 
  {
    _logger = logger;
    _logger.LogInformation("Constructor");
  }
}

It is tedious to manually type ILogger<MyClass> everywhere like ILogger<MyOtherClass> , ILogger<YetAnotherClass>ILogger<MyOtherClass>ILogger<YetAnotherClass>等任何地方手动键入ILogger<MyClass>很乏味

Is there a way to omit the <MyClass> part, but actually receive ILogger<MyClass> like below?有没有办法省略<MyClass>部分,但实际上接收ILogger<MyClass>如下所示? (Apparently in How to inject named logger generic ILogger<T> as ILogger into constructor using IServiceCollection and NLog the questioner claims to have a solution using Autofac, but I do not have enough Reputation to ask him in the comment) (显然在How to injection named logger generic ILogger<T> as ILogger into constructor using IServiceCollection and NLog中,提问者声称有一个使用 Autofac 的解决方案,但我没有足够的声誉在评论中问他)

public class MyClass
{
  private readonly ILogger _logger;

//public MyClass(ILogger<MyClass> logger)
  public MyClass(ILogger logger) // <<<<< notice that ILogger is used, instead of ILogger<T>
  {
    _logger = logger;
    _logger.LogInformation("Constructor");
  }
}

Thank you in advance.先感谢您。

I think my nuget package is exactly what you are looking for:我认为我的 nuget package 正是您正在寻找的:

Here you can read about how to use it:在这里您可以阅读有关如何使用它的信息:
https://github.com/gcsizmadia/EgonsoftHU.Extensions.Logging.Serilog.Autofac https://github.com/gcsizmadia/EgonsoftHU.Extensions.Logging.Serilog.Autofac

You can download it from here:你可以在这里下载:
https://www.nuget.org/packages/EgonsoftHU.Extensions.Logging.Serilog.Autofac https://www.nuget.org/packages/EgonsoftHU.Extensions.Logging.Serilog.Autofac

I also have a nuget package for Microsoft Logging + Autofac for the same purpose (ie omit the generic type parameter):我还有一个 nuget package 用于 Microsoft Logging + Autofac 用于相同目的(即省略通用类型参数):
https://github.com/gcsizmadia/EgonsoftHU.Extensions.Logging.Autofac https://github.com/gcsizmadia/EgonsoftHU.Extensions.Logging.Autofac

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何注入命名记录器泛型 ILogger<t> 使用 IServiceCollection 和 NLog 作为 ILogger 进入构造函数</t> - How to inject named logger generic ILogger<T> as ILogger into constructor using IServiceCollection and NLog 注入 ILogger<t> 成抽象 Class</t> - Inject ILogger<T> into an Abstract Class 为什么注入 ILoggerFactory 而不是 ILogger<t> ?</t> - why inject ILoggerFactory instead of ILogger<T>? 使用 ILogger<t> 没有构造函数注入</t> - Using ILogger<T> without constructor injection 如何注射ILogger <T> 依赖于ConfigureServices中的服务 - .net core 2.0 - How to inject ILogger<T> dependency to a service in ConfigureServices - .net core 2.0 如何将 ILogger 注入 EFCore DbContext - How to inject ILogger into EFCore DbContext 记录器<T> ? 默认为 null vs ILogger<T> 默认为 NullLogger<T> - ILogger<T>? defaulting to null vs ILogger<T> defaulting to NullLogger<T> 如何从 .NET Framework 初始化 .net Core ILogger 或 ILoggerFactory 并注入 Class 内置库中的构造函数.Net Core - How to initialize .net Core ILogger or ILoggerFactory from .NET Framework and inject to a constructor in Class library built in .Net Core 无法使用Autofac将ILogger注入类 - Unable to inject ILogger into classes using Autofac 配置自定义 ILogger 的日志级别<t></t> - Configuring loglevel of custom ILogger<T>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM