nreco / logging

Generic file logger for .NET Core (FileLoggerProvider) with minimal dependencies
MIT License
284 stars 56 forks source link

Logging not working with non-specialized ILogger interface #47

Closed richard-eade closed 1 year ago

richard-eade commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 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 importanthttps://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();

services.AddSingleton(logger);

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

- Reply to this email directly, view it on GitHubhttps://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 unsubscribehttps://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: @.**@.>>

VitaliyMF commented 1 year ago

@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 commented 1 year ago

Thank you.

I have it working with ILogger _logger already.

My solution is composed of many different services that all share common components. All the components are logging and I wanted to avoid the type parameter in all the common components just for logging.

Richard

From: Vitaliy Fedorchenko @.> Sent: Monday, November 28, 2022 3:19 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 importanthttps://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.

@richard-eadehttps://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Frichard-eade&data=05%7C01%7C%7C2a4f567c65114895ab5208dad1219f61%7C1b16ab3eb8f64fe39f3e2db7fe549f6a%7C0%7C0%7C638052239590188693%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=TNlSmbJqSp1CsTTyY%2BI7nMhezBFBZ50kbm2AasNv4F4%3D&reserved=0 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-logshttps://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Flearn.microsoft.com%2Fen-us%2Faspnet%2Fcore%2Ffundamentals%2Flogging%2F%3Fview%3Daspnetcore-7.0%23create-logs&data=05%7C01%7C%7C2a4f567c65114895ab5208dad1219f61%7C1b16ab3eb8f64fe39f3e2db7fe549f6a%7C0%7C0%7C638052239590188693%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Im2K%2FsnZ7kpw4QgIdP0MLX0PKfAlw3J5XjCA4vwGX64%3D&reserved=0

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());

}

}

- Reply to this email directly, view it on GitHubhttps://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnreco%2Flogging%2Fissues%2F47%23issuecomment-1328766625&data=05%7C01%7C%7C2a4f567c65114895ab5208dad1219f61%7C1b16ab3eb8f64fe39f3e2db7fe549f6a%7C0%7C0%7C638052239590188693%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=tZrefYXfsnTVoXpWVg%2FR2%2FxjnOL8CibaUxY3oQxlbvI%3D&reserved=0, or unsubscribehttps://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FA4MMZQBV6YALJ2BAOALN6ITWKR2JHANCNFSM6AAAAAASLOHMN4&data=05%7C01%7C%7C2a4f567c65114895ab5208dad1219f61%7C1b16ab3eb8f64fe39f3e2db7fe549f6a%7C0%7C0%7C638052239590188693%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=g9TwpsRVOpaxRrpdhecE1UMx5EydM2y7QANjx%2BJ9xrI%3D&reserved=0. You are receiving this because you were mentioned.Message ID: @.**@.>>

VitaliyMF commented 1 year ago

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 commented 1 year ago

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 importanthttps://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 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 GitHubhttps://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 unsubscribehttps://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: @.**@.>>

VitaliyMF commented 1 year ago

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.