17

All the examples I can find about using Serilog in an ASP .NET Core Web Application use Microsoft's ILogger<T> interface instead of using Serilog's ILogger interface.

How do I make it so that Serilog's ILogger can be injected via constructor, instead?

using Serilog;

public class HomeController : Controller
{
    ILogger logger;

    public HomeController(ILogger logger)
    {
        this.logger = logger;
    }

    public IActionResult Index()
    {
        this.logger.Information("Index was called");
        return View();
    }
}
10
  • 4
    You need to conficure ASP.NET Core to use Serilog as a provider, not inject Serilog's logger. If you google for Serilog ASP.NET Core the first result is the Serilog ASP.NET Core package that does just that Commented Jul 30, 2019 at 13:19
  • 1
    Seconding that, the idea is that you don't want to be come coupled to Serilog. If you inject Microsoft.Extensions.Logging.ILogger, there are adapters from that to all sorts of loggers. But if you inject Serilog everywhere and then you want something else it's more work. Commented Jul 30, 2019 at 13:36
  • 1
    Thanks @ScottHannen. Are you sure about that though? I mean, Serilog has Sinks to all kinds of loggers... I don't see any difference of being coupled to Serilog, or coupled to Microsoft's logger, other than personal preference - what am I missing? Commented Jul 30, 2019 at 14:01
  • 2
    You're right. It really is preference. I would tend to go as "generic" as possible. Commented Jul 30, 2019 at 14:06
  • 1
    Another viewpoint, maybe biased - in our apps, we use Serilog's logging interfaces exclusively (and the static Log class to avoid polluting constructors with irrelevant details); we hook up Serilog to MEL, but only third-party components/the framework use it, so we never see the MEL interfaces. Works nicely for us. Commented Jul 30, 2019 at 21:05

2 Answers 2

18

If you prefer ILogger instead of ILogger<HomeController>, you could try to register ILogger.

Here are two options to use Serialog.Information.

  1. Use Log.Logger

    Log.Logger.Information("Information Log from Log.Logger");
    
  2. Register ILogger

    //Startup.cs
    services.AddSingleton(Log.Logger);
    
    //Use
    public class HomeController : Controller
    {
        private readonly ILogger _logger;
        public HomeController(ILogger logger)
        {
            _logger = logger;
        }
        public IActionResult Index()
        {
            _logger.Information("Inform ILog from ILogger");
            return View();
        }        
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Tao Zhou.
8

You can install Serilog as the logger under the Microsoft logging framework by including the Serilog.Extensions.Logging package and including the following in your app startup:-

public void ConfigureServices(IServiceCollection services)
{
    services.AddLogging(x =>
    {
        x.ClearProviders();
        x.AddSerilog(dispose: true);
    });

    ...

Or, as an alternative to injecting, if you just want a reference to the Serilog logger, Serilog.Log has a static method Log to create a logger...

...
using Serilog;
...

namespace Test.Controllers
{
    public class TestController : Controller
    {
        private readonly static ILogger log = Log.ForContext(typeof(TestController));

        public TestController()
        {
            log.Debug("Test");
        }

2 Comments

upvoting the first part -- don't couple yourself to serilog in the controller.
The first part helped me. I didn't realize that needed to be done. I'm using the standard public HomeController(ILogger<HomeController> logger){_logger = logger;} to keep knowledge of the specific log library (Serilog) out of the controllers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.