question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Logging does not work when using Autofac DI

See original GitHub issue

Replacing 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:open
  • Created a year ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sungam3rcommented, Jul 7, 2023

@BluePositive My suggestion was to add virtual modifiers first.

0reactions
BluePositivecommented, May 18, 2023

I made this suggestion #1502 (comment)

I’m sorry for my confusion. Can we implement this suggestion at this moment, or do we need to add the virtual modifiers first?

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Injected ILogger using Autofac doesn't log anything on ...
I'm using ILogger abstraction of Microsoft.Extensions.Logging in other to do some logging. If I do the logging in the Run of the Azure ......
Read more >
C# Dependency Injection with Autofac - YouTube
Dependency Injection in C# can be a really confusing topic. Yet, when done right, Dependency Injection can be one of the best things...
Read more >
Autofac: Home
Autofac is an addictive Inversion of Control container for .NET Core, ASP.NET Core, .NET 4.5.1+, Universal Windows apps, and more. Quick Start Guide...
Read more >
Controlling Scope and Lifetime - Autofac's documentation!
Since the controller is registered as “instance per lifetime scope,” this instance will be shared by any components within that request but will...
Read more >
Introduction to Aspect-Oriented Programming (AOP) in . ...
Instead, we tell Autofac to intercept all calls to methods in a particular class with a LoggingInterceptor . This LoggingInterceptor then takes ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found