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.
Configure Serilog
Section titled “Configure Serilog”At MassTransit, we are big fans of Serilog and use this default configuration as a starting point in most projects.
dotnet add package Serilog.Extensions.Hostingdotnet add package Serilogdotnet add package Serilog.Sinks.ConsoleThen, 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); });});Configure other logging providers
Section titled “Configure other logging providers”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.