Search code examples
c#castle-windsorserilogmasstransit

Logging Mass Transit with Serilog


I have this console application project where I use EF6 with postgresql, Quartz and Mass Transit and as DI I use Castle Winsdor. The goal of the project is to check periodically a folder (or folders) for new files and process them (mostly by storing data in the DataBase).

I wanted to use a logging service for debug purposes and I came across Serilog. I managed to add it to Quartz and EF, but I have issues adding it to Mass Transit.

What I've done so far:

I've added Serilog as Logging for Castle.Core.Logging

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    container.Auto(Classes.FromThisAssembly());
    var factory = container.Resolve<ILoggerFactory>();
    container.AddFacility<LoggingFacility>(f => f.LogUsing(factory as SerilogFactory));
}

And then I added it to EF using a Logger Interceptor and to Quartz using Serilog settings.

But now I want to add it to my MassTransit Automatonymous, but I'm having trouble figuring it out.

First of all, I tried to add Serilog from the BusControl Configuration:

BusControl = Bus.Factory.CreateUsingInMemory(cfg =>
{
    cfg.UseSerilog(Log.Logger);  //Where `Log.Logger` is the Serilog Logger
});

But then I encountered a MassTransit.Logging.ILogger exception that was also discussed here.

In that thread I found that I should add Serilog (or any other logger) in a different way, by configuring the MassTransit LogContext like so LogContext.ConfigureCurrentLogContext(loggerFactory);. But the thing is, they are now using Microsoft.Extensions.Logging.Abstractions and I cannot convert my logger from Castle.Core.Logging.ILogger to Microsoft.Extensions.Logging.ILogger.

Is there any way to use my Castle.Core.Logging logger?


Solution

  • You need to use the Serilog.Extensions.Logging NuGet package, which adds ILoggerFactory support to Serilog. You can pass that interface to configure MassTransit via:

    LogContext.ConfigureCurrentLogContext(loggerFactory);
    

    MassTransit does not use any Castle facilities, however, there is a NuGet package for configuring MassTransit using ]Castle Windsor](https://masstransit-project.com/usage/containers/castlewindsor.html).

    Update: You can view a sample Serilog configuration in this sample