Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net-core

Created a blank ASP.NET Core 2.0 application. In Startup.cs, would like to log incoming requests. So in configure method, I am using Microsoft.Extensions.Logging.ILogger

  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger logger)
  {    
      app.Use(next =>
        {
            return async context =>
            {
                logger.LogInformation("Incoming request");
                await next(context);
                logger.LogInformation("Outgoing response");
            };
        });

However, when I build the project, its complaining

 An error occurred while starting the application.
 InvalidOperationException: No service for type 'Microsoft.Extensions.Logging.ILogger' has been registered.

Why and how should I register this service? Had it been my interface, it would have still made sense to do

services.AddScope

in ConfigureServices

like image 210
blue piranha Avatar asked Sep 12 '25 17:09

blue piranha


1 Answers

ILogger is always of a type, try change it like this:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger<Startup> logger)
  {    
      app.Use(next =>
        {
            return async context =>
            {
                logger.LogInformation("Incoming request");
                await next(context);
                logger.LogInformation("Outgoing response");
            };
        });
like image 143
Joe Audette Avatar answered Sep 15 '25 20:09

Joe Audette



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!