Skip to content

Logging Configuration

MassTransit uses Microsoft.Extensions.Logging for all log output. Therefore, any logging configuration that you apply to your application will be used by MassTransit.

At MassTransit, we are big fans of Serilog and use this default configuration as a starting point in most projects.

Terminal window
dotnet add package Serilog.Extensions.Hosting
dotnet add package Serilog
dotnet add package Serilog.Sinks.Console

Then, in Program.cs, configure Serilog as shown below.

using Serilog;
using Serilog.Events;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("MassTransit", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Fatal)
.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Fatal)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog();
builder.Services.AddMassTransit(x =>
{
x.AddServiceBusMessageScheduler();
x.UsingAzureServiceBus((context, cfg) =>
{
cfg.Host(serviceBusConnectionString);
cfg.UseServiceBusMessageScheduler();
cfg.ConfigureEndpoints(context);
});
});

For applications that are not using MassTransit’s container-based configuration (AddMassTransit) or for those with non-standard log configurations, it’s possible to explicitly configure MassTransit so that it uses a provided ILoggerFactory.

ILoggerFactory loggerFactory = CreateLoggerFactory();
var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
LogContext.ConfigureCurrentLogContext(loggerFactory);
});

This must be specified within the bus configuration so that the provided ILoggerFactory is used.