Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger`1[WebApplication1.Startup]'

前端 未结 5 2178
轮回少年
轮回少年 2021-02-05 12:29

I created an ASP.NET Core 3.0 Web Application with the default template in Visual Studio 2019 Preview 2.2 and tried to inject an ILogger in Startup:

namespace We         


        
5条回答
  •  日久生厌
    2021-02-05 13:01

    Did you have using ServiceCollection to register the Logger yet! It's should be here.

    serviceProvider.AddTransient(typeof(ILogger<>), (typeof(Logger<>));
    

    or you put, this is sample for Program.cs

    public class Program  
    {  
        public static void Main(string[] args)  
        {  
            BuildWebHost(args).Run();  
        }  
    
        public static IWebHost BuildWebHost(string[] args) =>  
            WebHost.CreateDefaultBuilder(args)  
                .ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Warning))  
                .UseStartup()  
                .Build();  
    }  
    

提交回复
热议问题