从 Class Libary .NET Core 3 中的非控制器类访问 ILogger [英] Accessing ILogger from non-Controller classes in Class Libary .NET Core 3

查看:18
本文介绍了从 Class Libary .NET Core 3 中的非控制器类访问 ILogger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将 .NET Framework 应用程序 (MVC) 迁移到 .NET Core 3 应用程序 (MVC).我们有一个场景如下:

We are migrating a .NET Framework application(MVC) to a .NET Core 3 application (MVC). We have a scenario as follows:

流程 1:由 ControllerA 实例化的 ClassX

Flow 1: ClassX instantiated by ControllerA

流程 2:ClassX 实例化的 ClassY 实例化的 ClassZ 实例化的 ClassD 实例化的 ClassD 由控制器 B 实例化

Flow 2: ClassX instantiated by ClassY instantiated by ClassZ instantiated by ClassD instantiated by Controller B

(ControllerA 和 ControllerB 是 MVC 项目的一部分.类 X、Y、Z、D 是 MVC 项目引用的类库的一部分.)

(ControllerA and ControllerB are part of the MVC Project. Classes X, Y, Z, D are part of a class library referenced by the MVC project. )

在旧的 .NET Framework 项目中,使用 log4net 并使用 LogManager.GetLogger 在每个类中创建静态 ILog 对象.但是 ASP.NET Core 使用 DI 原理.所以根据我的理解,ILoggerFactory 在启动时被注入到控制器 A 和 B 中.在流程 2 中,loggerFactory 可以从 ControllerB 传递到 ClassX,从 ClassX 传递到 ClassY,依此类推.

In the old .NET Framework project, log4net was used and static ILog objects were created in every class using LogManager.GetLogger. But ASP.NET Core uses DI principle. So from my understanding, the ILoggerFactory is injected into the Controllers A and B at Startup time. The loggerFactory can be passed from ControllerB to the ClassX, from ClassX to ClassY and so on, in Flow 2.

只有 ClassX 需要日志记录,流程 2 中的其他类 Y、Z 和 D 不需要.

Only ClassX needs logging and not the other classes Y, Z and D in Flow 2.

在这种情况下,是否有其他方法可以在不更改中间类构造函数(Y、Z、D)的情况下使用 ILogger 进行日志记录?

Is there an alternative approach for doing logging with ILogger in this scenario, without changing the intermediate class constructors(Y, Z, D)?

推荐答案

不要在控制器内部实例化依赖类,而是将此责任委托给 .NET Core 内置 DI 容器 - 这将帮助您将依赖类直接注入到必修课.

Instead of instantiating a depended class inside your controller, delegate this responsibility to .NET Core inbuilt DI container - This will help you to inject the depended class directly to the required class.

以下代码片段将帮助您如何在现有代码库中使用 DI.

Below code snippet will help you how to use DI in your existing codebase.

 public class Startup
 {
    ...
    public void ConfigureServices(IServiceCollection services)
    {      
      services.AddLogging();
      services.AddTransient<ClassX>();
      services.AddTransient<ClassY>();
      services.AddTransient<ClassZ>();
      services.AddTransient<ClassD>();
      ...
    }
}

public class ControllerB : ControllerBase
{
    private readonly ClassD classD;
    private readonly ILogger logger;

    public ControllerB(ClassD classD, ILogger<ControllerB> logger)
    {
        this.classD = classD;
        this.logger = logger;
    }
    ...
}

public class ClassD
{
   private readonly ClassZ classZ;

   public ClassD(ClassZ classZ)
   {
       this.classZ = classZ;
   }
} //Do the same thing for ClassZ and ClassY

public class ClassX
{
   private readonly ILogger logger;

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

这篇关于从 Class Libary .NET Core 3 中的非控制器类访问 ILogger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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