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

Middleware Debug logs are not forwarded to log4net provider. #70

Closed
fiseni opened this issue Sep 6, 2019 · 2 comments
Closed

Middleware Debug logs are not forwarded to log4net provider. #70

fiseni opened this issue Sep 6, 2019 · 2 comments

Comments

@fiseni
Copy link

fiseni commented Sep 6, 2019

No matter the configuration and how the log4net provider is registered, the middleware debug logs are not showed. Lets assume we have the following section in appsettings.json (we must explicitly define the Debug section, otherwise this provider has default level of Information). In this configuration we will notice a lot of debug messages from the middleware (routing configuration, entity framework migration logs, razor logs etc)

"Logging": {
  "LogLevel": {
    "Default": "Debug",
    "System": "Debug",
    "Microsoft": "Debug"
  },
  "Debug": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Debug",
      "Microsoft": "Debug"
    }
  }
}

Now, no matter how we register the log4net provider, these debug messages will never reach this provider. Just as an example, in the following output we see that Info level is forwarded but not the debug logs

Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware:Debug: Request matched endpoint 'PozitronDev.EShop.Web.Controllers.HomeController.Index (PozitronDev.EShop.Web)'
Microsoft.AspNetCore.Routing.EndpointMiddleware:Information: Executing endpoint 'PozitronDev.EShop.Web.Controllers.HomeController.Index (PozitronDev.EShop.Web)'
Microsoft.AspNetCore.Routing.EndpointMiddleware:16:30:09,574 [12] INFO - Executing endpoint 'PozitronDev.EShop.Web.Controllers.HomeController.Index (PozitronDev.EShop.Web)'

The platform is AspNet Core 2.2, and I have set the minimum level programmatically. I tested adding the provider during the host building process, I altered the IServiceCollection collection, altered the ILoggerFactory configuration. None of these worked.
Do anyone have any idea, anyone found a solution? or it's just me missing something :)

It's easy to replicate the issue, but anyway if you can't I'll set up a repository.

@fiseni
Copy link
Author

fiseni commented Sep 6, 2019

As soon I posted the issue, I found a solution :) I'm posting the answer, if anybody needs it.
We have to explicitly define the filters programmatically as following.

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
    return WebHost.CreateDefaultBuilder(args)
                    .UseStartup<Startup>()
                    .ConfigureLogging((hostingContext, logging) =>
                    {
                        //I assumed if clearing the providers, may clear the filter configuration, but it turned out it doesnt matter.
                        logging.ClearProviders();
                        logging.SetMinimumLevel(LogLevel.Debug);

                        //This won't work, no matter that you properly have configured everything.
                        //logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));

                        logging.AddFilter("Default", LogLevel.Debug);
                        logging.AddFilter("Microsoft", LogLevel.Debug);
                        logging.AddFilter("System", LogLevel.Debug);

                        //I'm adding the debug provider, just so we can compare, in production you can exclude it.
                        logging.AddDebug();
                        logging.AddLog4Net();
                    });
}

@huorswords
Copy link
Owner

Hello @fiseni ,

Oh! Thank you by your question and specially by your answer. It could be very helpful for me too in the future.

Regards

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

No branches or pull requests

2 participants