简体   繁体   中英

No service for type 'Microsoft.Extensions.Logging.ILoggingBuilder' has been registered

I am building brand new Web APIs with .NET core 2.2. In my Startup.cs I have set ILoggerFactory in configure method. When I do such a thing and add following code

loggerFactory.AddConsole();
loggerFactory.AddDebug();

I get information saying that this method is obsolete and it will be removed in future versions, instead I should use ILoggingBuilder .No problem, I have replaced with this new logging method and as soon as I start Web APIs I get error

InvalidOperationException: No service for type 'Microsoft.Extensions.Logging.ILoggingBuilder' has been registered.

and my output window shows this

Could not resolve a service of type 'Microsoft.Extensions.Logging.ILoggingBuilder' for the parameter 'loggingBuilder' of method.

I am new to .NET core, but am I missing something here? With ILoggerFactury, I did not have to register any services and logging would work just fine. Microsoft's documentation here is not really helpful.

Startup.cs looks like this:

public class Startup
{
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggingBuilder loggingBuilder)
    {
        loggingBuilder.AddConsole();
        loggingBuilder.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler();
        }
        app.UseMvc();

        //app.Run(async (context) =>
        //{
        //    await context.Response.WriteAsync("Hello World!");
        //});
    }
}

Program.cs looks like this:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

It's not working because you're not registering the logging components with your dependency injection container. There are two ways you can do this:

Either configure it as part of the CreateWebHostBuilder :

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureLogging((hostingContext, logging) =>
        {
            logging.AddConsole();
            logging.AddDebug();
        })
        .UseStartup<Startup>();

Or, alternatively, register it with your service collection under ConfigureServices :

services.AddLogging(logging =>
{
    logging.AddConsole();
    logging.AddDebug();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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