serilog / serilog-extensions-logging

Serilog provider for Microsoft.Extensions.Logging

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using Serilog with Microsoft.Extension.Logging (MEL)

henreman opened this issue · comments

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