Solving error - Unable to resolve service for type 'Serilog.ILogger'

Try to do following thing in your controller.

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

Which of the following two lines do you have in your controller?

using Serilog;
using Microsoft.Extensions.Logging;

If it's the former (using Serilog;) then that's probably your problem.

When you register the service, you're essentially saying "When a constructor asks for an Microsoft.Extensions.Logging.ILogger, pass them a Serilog instance because I'm using Serilog for my logging", but then in your constructor you aren't asking for Microsoft.Extensions.Logging.ILogger, you're asking for Serilog.ILogger and your application is getting confused, because you haven't defined an service to be injected for Serilog.ILoger, you've defined one for Microsoft.Extensions.Logging.ILogger

Remember that you are implementing Microsoft's ILogger interface: your constructor doesn't need to know that you're using Serilog... in fact, you don't want it to! The entire point is that you can swap Serilog out for a different logger at any time without having to change your code.

Change using Serilog; to using Microsoft.Extensions.Logging; in your constructor, and you'll be requesting the correct service type


If you need to use ILogger (from Serilog) instead of ILogger<HomeController> (from Microsoft.Extensions.Logging), you can register ILogger on your Startup class:

services.AddSingleton(Log.Logger);