A short post today; a colleague wanted to investigate a production issue when sending and receiving messages using MassTransit. He couldn’t find immediately how to change to capture more details about the MassTransit internals.
I had a look together with him. So for further reference you can find the answer here.
Using Serilog
On this specific project he was using Serilog so let me first show you how to change the log level for MassTransit when using Serilog:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Serilog; | |
Log.Logger = new LoggerConfiguration() | |
.MinimumLevel.Information() | |
//Override default log level | |
.MinimumLevel.Override("MassTransit", LogEventLevel.Debug) | |
.WriteTo.Console() | |
.CreateLogger(); | |
try | |
{ | |
Log.Information("Starting web application"); | |
var builder = WebApplication.CreateBuilder(args); | |
builder.Services.AddSerilog(); | |
var app = builder.Build(); | |
//Removed the other code | |
app.Run(); | |
} | |
catch (Exception ex) | |
{ | |
Log.Fatal(ex, "Application terminated unexpectedly"); | |
} | |
finally | |
{ | |
Log.CloseAndFlush(); | |
} |
Using Microsoft.Extensions.Logging
And here is the code changes required when using the default Microsoft.Extensions.Logging:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var builder = WebApplication.CreateBuilder(args); | |
builder.Logging | |
.SetMinimumLevel(LogLevel.Information) | |
.AddConsole() | |
// Override the default log level for MassTransit | |
.AddFilter("MassTransit", LogLevel.Debug); | |
var app = builder.Build(); | |
try | |
{ | |
app.Logger.LogInformation("Starting web application"); | |
//Removed the other code | |
app.Run(); | |
} | |
catch (Exception ex) | |
{ | |
app.Logger.LogError(ex, "Application terminated unexpectedly"); | |
} | |
finally | |
{ | |
} |
Remark: I would certainly recommend to have a look at the observability features of MassTransit as well and look to implement OpenTelemetry in your application.