Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to register ILogger with the decorater pattern #205

Open
arpit2408 opened this issue Jun 17, 2023 · 2 comments
Open

Not able to register ILogger with the decorater pattern #205

arpit2408 opened this issue Jun 17, 2023 · 2 comments

Comments

@arpit2408
Copy link

Hi @khellang trying to register my ILogger service like this:-

Method 1:

1: services.AddLogging();
2: services.Decorate<Extensions.Logging.ILogger, ExtensionsLoggerDecorator>();

It gives the following error at line 2:-
image

Method 2:

1: services.AddLogging();
2: services.AddSingleton<Extensions.Logging.ILogger, Extensions.Logging.ILogger>();
3: services.Decorate<Extensions.Logging.ILogger, ExtensionsLoggerDecorator>();

It passes to line 3 but gives the following error at hostBuilder.build()
image

Here is the definition of the ExtensionsLoggerDecorator class

public class ExtensionsLoggerDecorator : ILogger {
    // All interface classes implemented
}

I have installed the latest version https://www.nuget.org/packages/Scrutor and using .NET 6. I am trying to accomplish a wrapper/decorator for my ILogging implementation so that all logs pass through my custom class before getting logged by openTelemetry.

Please tell me what I am doing wrong here or is it a bug?

@arpit2408
Copy link
Author

I have looked at the answers here dotnet/runtime#36021 but didn't help.

@arpit2408
Copy link
Author

The compile time error is resolved if I use the decorator like below:-

services.AddLogging();
services.Decorate(typeof(ILogger), typeof(ExtensionsLoggerDecorator));

But now the decorator function isn't called when I call _logger.logInformation and instead extensions.logger is called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant