Register a Microsoft.Extensions.Logging.ILoggerProvider with EasyNetQ

By Mike Hadlow, published Aug 13, 2021

EasyNetQ is an opinionated fully featured client API for RabbitMQ. It provides extensive diagnostic logging capabilities via LibLog a logging abstraction that provides support for many common logging libraries. Unfortuntely LigLog doesn’t support Microsoft.Extensions.Logging, but this post provides an adaptor, EasyNetQMicrosoftExtensionsLogProvider that will allow EasyNetQ to log to a Microsoft.Extensions.Logging.ILoggerProvider.

Use it like this:

using EasyNetQ;
using EasyNetQ.Logging;

// ...
// loggerProvider is your Microsoft.Extensions.Logging.ILoggerProvider
LogProvider.SetCurrentLogProvider(new EasyNetQMicrosoftExtensionsLogProvider(loggerProvider));
// Call CreateBus after setting the current log provider
RabbitHutch.CreateBus(..);

Copy the code below into a file named EasyNetQMicrosoftExtensionsLogProvider.cs in your project.

using System;
using EasyNetQ.Logging;
using Microsoft.Extensions.Logging;

namespace YourNamespace
{
    public class EasyNetQMicrosoftExtensionsLogProvider : ILogProvider
    {
        private readonly ILoggerProvider loggerProvider;

        public EasyNetQMicrosoftExtensionsLogProvider(ILoggerProvider loggerProvider)
        {
            this.loggerProvider = loggerProvider ?? throw new ArgumentNullException(nameof(loggerProvider));
        }

        public Logger GetLogger(string name)
        {
            var logger = loggerProvider.CreateLogger(name);

            return (logLevel, messageFunc, exception, formatParameters) =>
            {
                if (messageFunc == null)
                {
                    return true;
                }

                var msLogLevel = logLevel switch 
                { 
                    EasyNetQ.Logging.LogLevel.Debug => Microsoft.Extensions.Logging.LogLevel.Debug,
                    EasyNetQ.Logging.LogLevel.Error => Microsoft.Extensions.Logging.LogLevel.Error,
                    EasyNetQ.Logging.LogLevel.Fatal => Microsoft.Extensions.Logging.LogLevel.Critical,
                    EasyNetQ.Logging.LogLevel.Info => Microsoft.Extensions.Logging.LogLevel.Information,
                    EasyNetQ.Logging.LogLevel.Trace => Microsoft.Extensions.Logging.LogLevel.Trace,
                    EasyNetQ.Logging.LogLevel.Warn => Microsoft.Extensions.Logging.LogLevel.Warning,
                    _ => Microsoft.Extensions.Logging.LogLevel.None
                };

                var message = messageFunc();

                if(exception is Exception)
                {
                    logger.Log(msLogLevel, exception, message, formatParameters);
                }
                else
                {
                    logger.Log(msLogLevel, message, formatParameters);
                }

                return true;
            };
        }

        public IDisposable OpenMappedContext(string key, object value, bool destructure = false)
            => NullDisposable.Instance;

        public IDisposable OpenNestedContext(string message)
            => NullDisposable.Instance;

        private class NullDisposable : IDisposable
        {
            internal static readonly IDisposable Instance = new NullDisposable();

            public void Dispose()
            { }
        }

    }
}

Hi, I’m Mike Hadlow. Software developer, architect, blogger and open source developer.

Find my old blog at Code Rant. This ran from 2005 to 2020 and has hundreds of posts.

All code on this blog is published under an MIT licence. You are free to copy it and use it for any purpose without attribution. There is no warranty whatsoever. All non-code text is copyright Mike Hadlow and cannot be reused without permission.

There are no cookies on this site

The GitHub repository for this site is here.