Rafał Ryszkowski
Rafał Ryszkowski

Reputation: 425

Logging with Serilog in MassTransit

I've tried to use MassTransit.SerilogIntegration nuget, but it looks like its last version is 5.5.6. I'm using MassTransit 7.1.4, and when I've installed this Serilog nuget, I got an exception that it can't find the ILog interface. Is there any way to use Serilog with the current version of MassTransit?

Upvotes: 3

Views: 3276

Answers (1)

Chris Patterson
Chris Patterson

Reputation: 33358

MassTransit uses ILoggerFactory from the container, so as long as the logging provider is configured and part of the container, it will be resolved and used. It doesn't matter if it is Serilog, or Microsoft Extensions Logging.

This sample uses Serilog – in fact almost all of my samples use it.

This can be configured at the Hosting level:

return Host.CreateDefaultBuilder(args)
.UseSerilog((host, log) =>
{
    if (host.HostingEnvironment.IsProduction())
        log.MinimumLevel.Information();
    else
        log.MinimumLevel.Debug();

    log.MinimumLevel.Override("Microsoft", LogEventLevel.Warning);
    log.WriteTo.Console();
})

Or at the program level:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Information()
    .MinimumLevel.Override("JobService", Serilog.Events.LogEventLevel.Debug)
    .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
    .MinimumLevel.Override("Microsoft.EntityFrameworkCore", Serilog.Events.LogEventLevel.Warning)
    .MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", Serilog.Events.LogEventLevel.Warning)
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .CreateLogger();

return Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.UseStartup<Startup>();
    })
    .UseSerilog();

These all use the Serilog.Extensions.Logging NuGet package. There are no longer log-specific packages for MassTransit.

Upvotes: 5

Related Questions