serilog / serilog-extensions-logging

Serilog provider for Microsoft.Extensions.Logging
Apache License 2.0
312 stars 103 forks source link

Using Serilog with Microsoft.Extension.Logging (MEL) #161

Closed henreman closed 4 years ago

henreman commented 4 years ago

Hi Guys,

I'm using Microsoft.Extension.Logging (MEL) to abstract the logging in my application.

Following is a working code using the MEL default logger (for Console).

What modifications do I need to do to enable Serilog in my application using MEL?

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;

public class Program
{
    private static Microsoft.Extensions.Logging.ILogger _logger;

    static void Main(string[] args)
    {
        Setup();

        _logger.LogDebug("test");
    }

    static void Setup()
    {
        //setup our DI
        var services = new ServiceCollection();

        services.AddLogging(logging =>
        {
            logging.AddConsole();
            logging.SetMinimumLevel(LogLevel.Debug);
        });

        var provider = services.BuildServiceProvider();

        _logger = provider.GetService<ILoggerFactory>().CreateLogger<Program>();
    }
}

Cheers

henreman commented 4 years ago

Found a working-example at: https://github.com/serilog/serilog-extensions-logging/blob/dev/samples/Sample/Program.cs

Closing the issue :)