-
Notifications
You must be signed in to change notification settings - Fork 55
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
Comments
How you have added FileLoggerProvider?
because dependencies from |
I added the logger normally.
.ConfigureLogging((hostContext, loggingBuilder) =>
{
loggingBuilder.AddFile(
hostContext.Configuration.GetSection("Logging"),
fileLoggerOpts =>
{
fileLoggerOpts.FormatLogFileName = fName =>
{
return string.Format(fName, DateTime.Now);
};
});
loggingBuilder.AddConsole();
loggingBuilder.AddDebug();
})
But the dependency injection couldn't resolve dependencies on ILogger until I added it to the container. E.g. void MyConstructor(ILogger logger)
ILogger<MyService> was resolved automatically but ILogger was not until I added it to the container. Once I did that all the references to ILogger _logger resolved and they all logged correctly to Console and Debug.
Richard
From: Vitaliy Fedorchenko ***@***.***>
Sent: Sunday, November 27, 2022 7:11 AM
To: nreco/logging ***@***.***>
Cc: EADE Richard M ***@***.***>; Author ***@***.***>
Subject: Re: [nreco/logging] Logging not working with non-specialized ILogger interface (Issue #47)
You don't often get email from ***@***.******@***.***>. Learn why this is important<https://aka.ms/LearnAboutSenderIdentification>
This email is not from Hexagon's Office 365 instance. Please be careful while clicking links, opening attachments, or replying to this email.
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.
-
Reply to this email directly, view it on GitHub<https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnreco%2Flogging%2Fissues%2F47%23issuecomment-1328244003&data=05%7C01%7C%7C7c985fc9b5144f254ad208dad078de31%7C1b16ab3eb8f64fe39f3e2db7fe549f6a%7C0%7C0%7C638051514798504829%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=kfvdsNPYnANiRkQ39qYcBvoBNsv5%2FnrKXO764CD3HGM%3D&reserved=0>, or unsubscribe<https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FA4MMZQCD36OYEH4ET77TP33WKNMXHANCNFSM6AAAAAASLOHMN4&data=05%7C01%7C%7C7c985fc9b5144f254ad208dad078de31%7C1b16ab3eb8f64fe39f3e2db7fe549f6a%7C0%7C0%7C638051514798504829%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=OvNnLoBK9%2BCMk7Q7Ggzm3G7wJVMnpQAtKfXVQ83H8VY%3D&reserved=0>.
You are receiving this because you authored the thread.Message ID: ***@***.******@***.***>>
|
@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
Have you tried to use |
If you find a reason why singleton ILogger doesn't work with FileLoggingProvider I'm ok to accept this PR. |
I agree. Since it's unusual it's not a defect.
The Console and Debug and Event loggers all work in this scenario. I was hoping the file logger would too. Serilog also works in this scenario but I wanted to avoid something that heavy.
Richard
From: Vitaliy Fedorchenko ***@***.***>
Sent: Tuesday, November 29, 2022 3:30 AM
To: nreco/logging ***@***.***>
Cc: EADE Richard M ***@***.***>; Mention ***@***.***>
Subject: Re: [nreco/logging] Logging not working with non-specialized ILogger interface (Issue #47)
You don't often get email from ***@***.******@***.***>. Learn why this is important<https://aka.ms/LearnAboutSenderIdentification>
This email is not from Hexagon's Office 365 instance. Please be careful while clicking links, opening attachments, or replying to this email.
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).
-
Reply to this email directly, view it on GitHub<https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnreco%2Flogging%2Fissues%2F47%23issuecomment-1330335918&data=05%7C01%7C%7Cdec7e910e9694d0e8d7b08dad1ec480c%7C1b16ab3eb8f64fe39f3e2db7fe549f6a%7C0%7C0%7C638053110085889400%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=im7D9vjM46fB9h3hSjPoRgI72d75kQqI4ZfCOXTO2co%3D&reserved=0>, or unsubscribe<https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FA4MMZQB534Z6J2NEFDGVN6LWKXEJJANCNFSM6AAAAAASLOHMN4&data=05%7C01%7C%7Cdec7e910e9694d0e8d7b08dad1ec480c%7C1b16ab3eb8f64fe39f3e2db7fe549f6a%7C0%7C0%7C638053110085889400%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=bvRMOiFQWXkwNBM7sOGoYCwDIySE87HvB1GzJp65j0w%3D&reserved=0>.
You are receiving this because you were mentioned.Message ID: ***@***.******@***.***>>
|
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. |
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");
}
The text was updated successfully, but these errors were encountered: