How to register ILogger for injection in ASP.NET MVC 6

42,388

Solution 1

I assumed that services.AddLogging(); was doing the right thing and registering ILogger. After looking at the source (https://github.com/aspnet/Logging/blob/d874c5726e713d3eb34938f85faf7be61aae0f2a/src/Microsoft.Framework.Logging/LoggingServiceCollectionExtensions.cs) I found that it's actually registering ILogger<>. Changing the signature of ILogger to ILogger<HomeController> makes the above example work.

public class HomeController : 
    Controller
{
    ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger) 
    {
        _logger = logger;
    }

    // ...
}

Thanks to @Steve for setting me on the right track to find this.

Solution 2

The services.AddLogging(); didn't worked for me, so I added these two statements to ConfigureServices:

services.AddSingleton<ILoggerFactory, LoggerFactory>();
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));

Now the DI container is happy and everything works.

Share:
42,388
Travis
Author by

Travis

Updated on July 09, 2022

Comments

  • Travis
    Travis almost 2 years

    I have a ASP.NET MVC 6 (beta-4) app.

    public void ConfigureServices(IServiceCollection services)
    {
        // Logging
        services.AddLogging();
    
        // ...
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory)
    {
        // Add the console logger.
        loggerfactory.AddConsole(minLevel: LogLevel.Warning);
    
        // ...
    }
    

    And I have a controller...

    public class HomeController : 
        Controller
    {
        ILogger _logger;
    
        public HomeController(ILogger logger) 
        {
            _logger = logger;
        }
    
        // ...
    }
    

    But when I'm not getting the service registered correctly somehow: InvalidOperationException: Unable to resolve service for type 'Microsoft.Framework.Logging.ILogger' while attempting to activate 'HomeController'.. What am I doing wrong with the registering the logger?

  • b.pell
    b.pell about 8 years
    This works. I read they are going to alter RC2 to have <T> return whatever T is (like <AppSpecificLogger>) so you can actually get your implementation and not just the interface methods. Currently, you can put anything in there and it works, looks like it ignores it. I tested ILogger<IMemoryCache> just to test that theory and it worked.
  • VisualBean
    VisualBean about 8 years
    What T is actually used for, is the logging context. - you call ILogger <T> where T would usually be the calling type., but could anything really.
  • stankovski
    stankovski almost 8 years
    You can also specify ILoggerFactory as described here docs.asp.net/en/latest/fundamentals/logging.html
  • Sandro Stadler
    Sandro Stadler almost 7 years
    Really the signature was the issue. This have been working for me. ILogger<HomeController>. Thanks a lot.
  • Ben Humphrey
    Ben Humphrey about 6 years
    This just saved me after a couple hours of being dumbfounded.
  • Charles Burns
    Charles Burns about 2 years
    @Travis You saved me a trip to the asylum.