Logging does not work when using Autofac DI
See original GitHub issueReplacing the default NoopLogger
, with e.g. a SerilogLoggerAdapter
does not work if you use the RegisterEasyNetQ
extension method from the EasyNetQ.DI.Autofac
package for registering EasyNetQ in an Autofac container.
When running this example, the resolved IBus
at the end will be using the NoopLogger
instead of the SerilogLoggerAdapter
, which was registered by calling EnableSerilogLogging
:
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterInstance(new LoggerConfiguration().CreateLogger()).As<ILogger>();
builder.RegisterEasyNetQ("<connectionstring>", sr => sr.EnableSerilogLogging());
using IContainer container = builder.Build();
IBus bus = container.Resolve<IBus>();
A similar example using the Microsoft DI integration instead works as expected, and the IBus
is using the SerilogLoggerAdapter
:
ServiceCollection services = new ServiceCollection();
services.AddSingleton<ILogger>(new LoggerConfiguration().CreateLogger());
services.RegisterEasyNetQ("<connectionstring>", sr => sr.EnableSerilogLogging());
await using ServiceProvider provider = services.BuildServiceProvider();
IBus bus = provider.GetRequiredService<IBus>();
The issue is related to this Autofac issue: https://github.com/autofac/Autofac/issues/958.
The IfNotRegistered
method does not work for open generics, which is what the AutofacAdapter.TryRegister
method attempts to do: https://github.com/EasyNetQ/EasyNetQ/blob/e7565c520fca67d3fd8edf02d977bc078293bedc/Source/EasyNetQ.DI.Autofac/AutofacAdapter.cs#L98-L103
The suggested fix from Autofac is to change the type passed into IfNotRegistered
to be a closed generic instead, but that is not easy to do here, as the TryRegister
method just has the Type
objects.
Our workaround for now is to not use the Autofac DI integration.
Another workaround is to call EnableSerilogLogging
after calling RegisterEasyNetQ
, so that the Serilog logger gets registered last and then replaces the Noop logger.
EasyNetQ Version: 7.2.1
Issue Analytics
- State:
- Created a year ago
- Comments:14 (6 by maintainers)
Top GitHub Comments
@BluePositive My suggestion was to add virtual modifiers first.
I’m sorry for my confusion. Can we implement this suggestion at this moment, or do we need to add the virtual modifiers first?