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

查看:699
本文介绍了从类库.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:由ControllerA实例化的ClassX ClassY实例化的ClassY实例,Controller B实例化的ClassD实例

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中。 loggerFactory可以在流程2中从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需要记录,而其他Y,Z类不需要和D在流程2中。

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

在这种情况下,是否有一种使用ILogger进行日志记录的替代方法,而无需更改中间类的构造函数(Y,Z,D)? / p>

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;
   }
}

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

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