-1

I am usually using injection with Serilog and ILogger but in this case I hade to create my own ILogger like this :

private static readonly ILogger _logger = Serilog.Log.ForContext<MyClass>() as ILogger;

I do however need a Microsoft ILogger instead, is there any way to create a Microsoft ILogger from Serilog or to convert a existing Serilog ILogger to Microsoft ILogger?

Edit 1: To be clear, the project is a .net microservice that use "IHostBuilder.UseSerilog" to feed the injection of Microsoft ILogger to classes. In this specific case I can however not have a ILogger injected, this would create a circular relation. So to solve this I have to manually create it within the context(example above).

The problem is that this creates a Serilog ILogger while the method I need to use takes a Microsoft ILogger.

So either I need to convert the Serilog ILogger to a Microsoft ILogger or even better, get Serilog to create a Microsoft ILogger.

Actually, best would be if I could avoid adding more Serilog stuff into the code, it makes it harder if the log system later need to be switched.

Edit 2:

I have tried this :

var logger1 = Serilog.Log.ForContext<MessageQueueSink>() as Serilog.ILogger;

            logger1.Information("test1");

            var loggerFactorytest = new LoggerFactory()
                .AddSerilog(logger1);

            var logger2 = loggerFactory.CreateLogger("Logger");

            logger2.LogInformation("test2");

The first "test1" is shown in the output but the "test2" is not. its now leaning more and more toward excluding logging from this part of the code.

Banshee
  • 15,376
  • 38
  • 128
  • 219
  • Does this answer your question? [Use Serilog with Microsoft.Extensions.Logging.ILogger](https://stackoverflow.com/questions/61544047/use-serilog-with-microsoft-extensions-logging-ilogger) – Glen Thomas Mar 04 '22 at 11:20
  • @GlenThomas, not not really, Im already using Serilog injections with Microsofts ILogger in my application. But in this part I can´t use the injection but I still need to use the serilog and its configuration for the application and its context. The _logger in my example are working great, I get the log data the same way as if it would be injected. The problem is however that I need to use methods that only take Microsoft ILogger so If I could create a Microsoft ILogger from Serilog or convert it would solve the problem easy. I dont get the downvote? What is the problem with the question? – Banshee Mar 04 '22 at 13:37

1 Answers1

1

I did not find a way to get a MS ILogger from Serilog or translate a Serilog ILogger to a MS ILogger so instead I created a custom MS ILogger that wrapped a Serilog ILogger like this :

public class CustomSerilogger : Microsoft.Extensions.Logging.ILogger
    {
        private readonly ILogger _logger;
        public CustomSerilogger(ILogger logger)
        { _logger = logger; }

        public IDisposable BeginScope<TState>(TState state) => default!;

        public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) { return _logger.IsEnabled(LogLevelToLogEventLevel(logLevel)); }

        public void Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
                return;

            _logger.Write(LogLevelToLogEventLevel(logLevel), exception, state.ToString());
        }

        private LogEventLevel LogLevelToLogEventLevel(Microsoft.Extensions.Logging.LogLevel loglevel)
        {
            switch(loglevel)
            {
                case Microsoft.Extensions.Logging.LogLevel.Debug:
                    return LogEventLevel.Debug;
                case Microsoft.Extensions.Logging.LogLevel.Information:
                    return LogEventLevel.Information;
                case Microsoft.Extensions.Logging.LogLevel.Warning:
                    return LogEventLevel.Warning;
                case Microsoft.Extensions.Logging.LogLevel.Error:
                    return LogEventLevel.Error;
                case Microsoft.Extensions.Logging.LogLevel.Critical:
                    return LogEventLevel.Fatal;
                case Microsoft.Extensions.Logging.LogLevel.None:
                    return LogEventLevel.Verbose;
                case Microsoft.Extensions.Logging.LogLevel.Trace:
                    return LogEventLevel.Verbose;
            }
            return LogEventLevel.Verbose;
        }
    }
Banshee
  • 15,376
  • 38
  • 128
  • 219