简体   繁体   English

dotnet core-登录类库

[英]dotnet core-Logging in class library

is it possible to use Microsoft.Extensions.Logging like use logging in controllers(put in constructor and framework handle it with DI), in class library which my ASP.NET Core web application use that library? 是否可以在Microsoft ASP.NET Core Web应用程序使用该库的类库中使用Microsoft.Extensions.Logging像在控制器中使用日志记录一样(在构造函数和框架中使用DI进行处理)? and how instantiate class and use method? 以及如何实例化类和使用方法?

public class MyMathCalculator
{
    private readonly ILogger<MyMathCalculator> logger;

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

    public int Fact(int n)
    {
        //logger.LogInformation($"Fact({n}) called.");
        if (n == 0)
        {
            return 1;
        }
        return Fact(n - 1) * n;
    }
}

Taked from a previous answer : 取自先前的答案

...That is the magic of dependency injection, just let the system create the object for you, you just have to ask for the type. ...这就是依赖注入的魔力,只需让系统为您创建对象,您只需要询问类型即可。

This is also a big topic, ... basically, all you have to do is to define classes as dependencies, so, when you ask for one, the system itself check the dependencies, and the dependencies of that objects, until resolves all the tree of dependencies. 这也是一个大话题,...基本上,您要做的就是将类定义为依赖项,因此,当您请求一个类时,系统本身会检查依赖项以及该对象的依赖项,直到解决所有问题为止。依赖树。

With this, if you need one more dependency latter in your class, you can add directly but you do not need to modify all the classes that uses that class. 这样,如果您的类中又需要一个依赖项,则可以直接添加,但不需要修改使用该类的所有类。

To use this in the controller, please check the official docs , you just have to add you dependencies to the constructor, and win!, basically two parts: 要在控制器中使用此功能,请检查官方文档 ,您只需将依赖项添加到构造函数中,然后赢得!,基本上分为两部分:

Add in your Startup.class 添加您的Startup.class

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddTransient<MySpecialClassWithDependencies>();
    ...
}

Then in your controller: 然后在您的控制器中:

public class HomeController : Controller
{
    private readonly MySpecialClassWithDependencies _mySpecialClassWithDependencies;

    public HomeController(MySpecialClassWithDependencies mySpecialClassWithDependencies)
    {
        _mySpecialClassWithDependencies = mySpecialClassWithDependencies;
    }

    public IActionResult Index()
    {
        // Now i can use my object here, the framework already initialized for me!
        return View();
    }

This sould be no different if you library class is in other project, at the end of the day you will be adding the class to the startup, that is how asp net knows what to load. 如果您将库类放在其他项目中,这也没什么不同,最终,您将把类添加到启动中,这就是asp net知道要加载什么的方式。

If you want your code clean, you can use an Extension method to group all your declarations and the just calling services.AddMyAwesomeLibrary() , for example: 如果您希望代码干净,可以使用Extension方法对所有声明和刚刚调用的services.AddMyAwesomeLibrary()进行分组。AddMyAwesomeLibrary services.AddMyAwesomeLibrary() ,例如:

In your awesomeLibraryProject: 在您的awesomeLibraryProject中:

public static class MyAwesomeLibraryExtensions
{
    public static void AddMyAwesomeLibrary(this IServiceCollection services)
    {
        services.AddSingleton<SomeSingleton>();
        services.AddTransient<SomeTransientService>();
    }
}

And in your Startup 并在您的启动中

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddMyAwesomeLibrary();
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM