How do I log from other classes than the controller in ASP.NET Core?

后端 未结 3 1701
悲&欢浪女
悲&欢浪女 2021-02-04 03:34

ASP.NET Core has built-in support for logging, but the documentation states that logging should be done by requesting an ILogger via dependency injection, i.e. by a

3条回答
  •  后悔当初
    2021-02-04 03:49

    Regarding using logging in any component:

    Adding logging to a component in your application is done by requesting either an ILoggerFactory or an ILogger via Dependency Injection. If an ILoggerFactory is requested, a logger must be created using its CreateLogger method.

    If your CustomClass is a data container (DTO class), it should not know about logging, but just contain data.

    For other classes with names like "Service" , "Provider", "Handler" and so on, the best practices is to resolve instances using dependency injection. In general, you should use DI wherever it is possible, as it’s a technique for achieving loose coupling between objects and their collaborators or dependencies. For more information, the following question might be interesting: Should I use Dependency Injection or static factories?

    So simply add ILogger to its constructor (actually the same way, as you do for controllers), as .NET Core supports only constructor injection by default:

    public class CustomClass
    {
        private readonly ILogger _logger;
    
        public CustomClass(ILogger logger)
        {
            _logger = logger;
        }
    }
    

    ASP.NET Core’s built-in dependency injection container will automatically resolved transient dependencies. So if your controller has class A as a dependency that uses class B where you want to log something, your code can be something like this:

    public class MyController
    {
        public MyController(ClassA classA)
        { ... }
    }
    
    public class ClassA
    {
        public ClassA(ClassB classB)
        { ... }
    }
    
    public class ClassB
    {
        private readonly ILogger _logger;
    
        public ClassB(ILogger logger)
        {
            _logger = logger;
        }
    
        public void DoSomethingWithLogging()
        {
            // use _logger
        }
    }
    

    Note that you also need to register the dependencies in Startup using IServiceCollection.Add… methods:

    public void ConfigureServices(IServiceCollection services)
    {
        // ...
    
        services.AddTransient();
        services.AddTransient();
    }
    

    Built-in logging support means that .NET Core out of the box knows about and is using built-in abstractions for logging. This is mainly done by ILoggerFactory and ILoggerProvider interfaces.

    /// 
    /// Represents a type used to configure the logging system and create instances of  from
    /// the registered s.
    /// 
    public interface ILoggerFactory : IDisposable
    
    // 
    /// Represents a type that can create instances of .
    /// 
    public interface ILoggerProvider : IDisposable
    

    Using your own ILoggerProvider implementation, you can add your own logger that can do whatever you want. You can check the NLog logger implementation as working example.

    And thanks to ILoggerFactory, you can simply configure your Logger for project-specific purposes:

    To configure logging in your ASP.NET Core application, you should resolve ILoggerFactory in the Configure method of your Startup class. ASP.NET Core will automatically provide an instance of ILoggerFactory using Dependency Injection when you add a parameter of this type to the Configure method.

提交回复
热议问题