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

Logging not working with non-specialized ILogger interface #47

Closed
richard-eade opened this issue Nov 25, 2022 · 7 comments
Closed

Logging not working with non-specialized ILogger interface #47

richard-eade opened this issue Nov 25, 2022 · 7 comments
Labels

Comments

@richard-eade
Copy link

When I use the non-specialized ILogger interface the file logging doesn't log.
...
var logger = serviceProvider.GetService<ILogger>();
services.AddSingleton(logger);
...
void MyClass(ILogger logger)
{
logger.LogInformation("This doesn't file log but the console and debug loggers do log it");
}

@VitaliyMF
Copy link
Contributor

How you have added FileLoggerProvider?
Normally you never add ILogger as a service like that

var logger = serviceProvider.GetService<ILogger>();
services.AddSingleton(logger);

because dependencies from ILogger and ILogger<T> are resolved by Microsoft.Extensions.Logging infrastructure.

@richard-eade
Copy link
Author

richard-eade commented Nov 27, 2022 via email

@VitaliyMF
Copy link
Contributor

VitaliyMF commented Nov 28, 2022

@richard-eade FileLoggerProvider works like any other logging provider - in the same way as AddConsole, there are no any differences here.

In any way, if ILogger is not resolved by Microsoft.Extensions.Logging services, this means that you should not use it as a dependency - take a look to the example from https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-7.0#create-logs

public class AboutModel : PageModel
{
    private readonly ILogger _logger;

    public AboutModel(ILogger<AboutModel> logger)
    {
        _logger = logger;
    }


    public void OnGet()
    {
        _logger.LogInformation("About page visited at {DT}", 
            DateTime.UtcNow.ToLongTimeString());
    }
}

Have you tried to use ILogger<> dependency? If it also doesn't work, this may indicate that your FileLoggerProvider is configured to filter log messages by the loglevel -- please check your "Logging" section.

@richard-eade
Copy link
Author

richard-eade commented Nov 28, 2022 via email

@VitaliyMF
Copy link
Contributor

If you find a reason why singleton ILogger doesn't work with FileLoggingProvider I'm ok to accept this PR.
However, since you use Microsoft.Extensions.Logging infrastructure in an unusual way, I not sure that this is really a bug inside FileLoggingProvider. I don't see any reason why you cannot use dependency on ILogger<Program> instead of ILogger (that is not resolved by Microsoft.Extensions.Logging -- so this is not a correct way to use logging infrastructure, and all issues that may appear when you use one shared ILogger for all requests they are not bugs actually).

@richard-eade
Copy link
Author

richard-eade commented Nov 29, 2022 via email

@VitaliyMF
Copy link
Contributor

I was hoping the file logger would too.

Implementation of FileLoggerProvider is similar to standard Console logger. This is open source - so if you can determine the reason why FileLoggerProvider doesn't work when one shared ILogger instance is used in the app I can do necessary changes to fix that. Currently I don't have free time to do that investigation by myself.

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

No branches or pull requests

2 participants