Unable to resolve ILogger from Microsoft.Extensions.Logging

前端 未结 7 1025
太阳男子
太阳男子 2021-02-02 04:56

I\'ve configured my console application\'s Main like so

var services = new ServiceCollection()
                .AddLogging(logging => logging.Add         


        
7条回答
  •  伪装坚强ぢ
    2021-02-02 05:29

    ILogger is no longer registered by default but ILogger is. If you still want to use ILogger you can registered it manually with the following (Startup.cs):

        public void ConfigureServices(IServiceCollection services)
        {
            var serviceProvider = services.BuildServiceProvider();
            var logger = serviceProvider.GetService>();
            services.AddSingleton(typeof(ILogger), logger);
            ...
         }
    

    Where AnyClass can be something generic, such as:

         public class ApplicationLogs
         {
         }
    

    So:

            public void ConfigureServices(IServiceCollection services)
            {
                var serviceProvider = services.BuildServiceProvider();
                var logger = serviceProvider.GetService>();
                services.AddSingleton(typeof(ILogger), logger);
                ...
             }
    

    ILogger will now resolve via constructor injection

提交回复
热议问题