简体   繁体   English

我可以在没有 LoggerFactory 的情况下直接注入 ILogger

[英]Can I directly Inject ILogger without LoggerFactory

From the IoC perspective, we should be able to easily setup and choose the implementation of a interface.从 IoC 的角度来看,我们应该能够轻松设置和选择接口的实现。

If I create custom implementation of ILogger , can I directly injected it to controller without the ILoggerFactory-ILoggerProvider-ILogger structure?如果我创建ILogger自定义实现,是否可以将它直接注入控制器而不使用ILoggerFactory-ILoggerProvider-ILogger结构?

In this way I can have less setting overhead and more control on the Logger.通过这种方式,我可以减少设置开销并更好地控制 Logger。 For example, I can cast and access the CustomProperty .例如,我可以投射和访问CustomProperty In contrast, the logger created by ILoggerFactory-ILoggerProvider-ILogger chain will maintain a collection of ILogger s and I can not access my CustomLogger any more.相比之下,由ILoggerFactory-ILoggerProvider-ILogger链创建的记录器将维护一个ILogger的集合,我无法再访问我的CustomLogger

The code will look like below.代码如下所示。 Is this a feasible solution?这是一个可行的解决方案吗?

Define CustomLogger定义自定义记录器

public class CustomLogger : ILogger
{
   public CustomPropertyType CustomProperty;

   public CustomLogger(){...}
   public IDisposable BeginScope<TState>(TState state){...}
   public bool IsEnabled(LogLevel logLevel){...}

   public void Log<TState>(
            LogLevel logLevel,
            EventId eventId,
            TState state,
            Exception exception,
            Func<TState, Exception, string> formatter)
   {
      // Use some info from CustomProperty in while sending the log
   }
}

Setup the Dependency Injection设置依赖注入

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<ILogger, CustomLogger>();
    //Other logic
}

Access the ILogger with CustomLogger implementation使用CustomLogger实现访问ILogger

public ExampleController(ILogger logger)
{
    this.logger = logger;
    this.customProperty = (CustomLogger)logger.CustomProperty;
}

Yes, you need a provider.是的,您需要提供者。 You can add this in the Startup class of your .NET Core application.您可以将其添加到 .NET Core 应用程序的Startup类中。 Such code shall look like this:此类代码应如下所示:

loggerFactory.AddProvider(new CustomLoggerProvider(new CustomLoggerConfiguration()));

Here is an explanation of such usage. 是对这种用法的解释。 And here is an example. 是一个例子。

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

相关问题 如何将依赖项注入 asp.net core 2.0 中的自定义 ILogger? - How can I inject dependencies into a custom ILogger in asp.net core 2.0? 如何使用新的 DI 将 ILogger 注入使用 IWebJobsStartup 的 Azure 函数? - How can I use the new DI to inject an ILogger into an Azure Function using IWebJobsStartup? 如何将 LoggerFactory 的实例传递给 ActionFilterAttribute - How can I pass an instance of LoggerFactory to ActionFilterAttribute 注入 ILogger<t> 进入只需要 ILogger 的构造函数</t> - Inject ILogger<T> into constructor that only expects ILogger 注入 ILogger<t> 成抽象 Class</t> - Inject ILogger<T> into an Abstract Class 如何将 ILogger 注入 EFCore DbContext - How to inject ILogger into EFCore DbContext 使用 Azure Function V3,如何将现有的 Ilogger 注入新的 class 文件? - Using Azure Function V3, how do I inject existing Ilogger into new class file? 如何将自动映射器直接注入到控制器下方的存储库层 - How can I Inject Auto Mapper directly in to Repository Layer, below the Controller 我可以在没有界面的情况下使用Windsor注入组件吗 - Can i inject component without interface use windsor 无法使用Autofac将ILogger注入类 - Unable to inject ILogger into classes using Autofac
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM