We're currently running an ASP.NET Core 3 web app that uses MassTransit. We've chosen Serilog for our logging library and it works great for the most part. We've picked up a small edge case where it cannot log certain errors. In our Program.cs
(Code snippet below) we initialize the static logger while the service is still starting up and it logs to the console with no problems
If an exception is thrown in the ConfigureServices
method in the Startup.cs
the service is killed and the exception is printed to the console as expected. However, if an exception is thrown in the MassTransit AddBus()
method nothing is logged. After a little bit of debugging, we saw that the static logger we set up in Program.cs
is being replaced with a SilentLogger
and we believe it could be something internal in MassTransit that's causing this
The MassTransit initialization code can be found below as well
public class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
try
{
Log.Information("Starting up app");
CreateHostBuilder(args).Build().Run();
}
catch (Exception e)
{
Log.Fatal(e, "Host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
}
}
serviceCollection.AddMassTransit(configurator =>
{
configurator.AddConsumers(consumerAssembly());
var queueName = appSettings.QueueName;
configurator.AddBus(provider =>
{
MessageDataDefaults.ExtraTimeToLive = TimeSpan.FromDays(1);
MessageDataDefaults.Threshold = 2000;
MessageDataDefaults.AlwaysWriteToRepository = false;
return Bus.Factory.CreateUsingRabbitMq(cfg =>
{
var configHost = appSettings.RabbitMq.Host;
var configUser = appSettings.RabbitMq.Username;
var configPassword = appSettings.RabbitMq.Password;
cfg.Host(new Uri(configHost), h =>
{
h.Username(configUser);
h.Password(configPassword);
});
cfg.ReceiveEndpoint(queueName, ec => ec.ConfigureConsumers(provider));
});
});
serviceCollection.AddSingleton<IHostedService, BusHostedService>();
});
Any exception thrown within the AddBus
method will cause this issue. Here are the versions we're currently using:
- MassTransit 7.1.4
- MassTransit RabbitMq 7.1.4
- Serilog 2.10.0
Perhaps someone here has come across this issue before? Any assistance will be appreciated ??
question from:
https://stackoverflow.com/questions/66045967/serilog-static-logger-is-silentlogger-when-theres-an-exception-in-masstransit-a 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…