rebus-org / Rebus.Serilog

:bus: Serilog logging integration for Rebus
https://mookid.dk/category/rebus
Other
8 stars 6 forks source link

Rebus not using global serilog instance? #2

Open ghost opened 3 years ago

ghost commented 3 years ago

We have a asp.net core 3 web api which is listening to azure servicebus queues using Rebus. We have setup serilog according to the setup guides, but it seems that Rebus creates its own instance of ILogger instead of using the one we create in Program.cs.

We have tried getting the current instance from DI like below, but it does not seem to work.

var logger = services.BuildServiceProvider().GetService();

services.AddRebus((options, serviceProvider) => options .Logging(l => l.Serilog(logger)) .Transport(t => t.UseAzureServiceBus(configuration["RebusConfig:AzureServiceBusConnectionString"], configuration["RebusConfig:Order:InputQueueName"]).AutomaticallyRenewPeekLock())

Is there someway to make sure Rebus uses the same serilog instance as the rest of the app?

The problem that this makes is that we have a Agent running on the server which injects traceid and such into the logcontext, which only works with logs outside of Rebus with this setup.

mookid8000 commented 3 years ago

Could you maybe point to the place in the code, where you believe this goes wrong? You must be hitting this configuration method: 👉 https://github.com/rebus-org/Rebus.Serilog/blob/master/Rebus.Serilog/Config/SerilogConfigurationExtensions.cs#L23-L31

(btw. the correct way to initialize the logger, since you're using Microsoft DI, is to resolve the logger with the passed-in service provider:

services.AddRebus((options, serviceProvider) => 
    options
        .Logging(l => l.Serilog(serviceProvider.GetRequiredService<ILogger>()))
        .Transport(t => t.UseAzureServiceBus(configuration["RebusConfig:AzureServiceBusConnectionString"], configuration["RebusConfig:Order:InputQueueName"]).AutomaticallyRenewPeekLock()
);

)