Using .NET 6 Core for a console app and I can't get debug logging working (nothing is displayed). And, I have added Microsoft.Extensions.Logging.Console to the project.
You'll see two ways below that I've attempted to get a reference to the logger.
Notice that logger.LogInformation does work, but logger.LogDebug does not.
Question: How do I get LogDebug working?
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
var serviceProvider = new ServiceCollection()
.AddLogging(builder => {
builder.ClearProviders();
builder.AddConsole();
builder.AddDebug();
})
.BuildServiceProvider();
// I've tried this
var logger = serviceProvider.GetService<ILogger<Program>>();
// And this
var logger = serviceProvider.GetService<ILoggerFactory>().CreateLogger<Program>>();
logger.LogDebug("hello world"); // <-- This DOESN'T work
logger.LogInformation("something"); // <-- This DOES work
Just to have here complete working example for .NET Core 6.
You should add packages Microsoft.Extensions.Logging.Console to the project.
(In my case its version is 6.0.0)
Program.cs
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
ServiceProvider serviceProvider = new ServiceCollection()
.AddLogging((loggingBuilder) => loggingBuilder
.SetMinimumLevel(LogLevel.Trace)
.AddConsole()
)
.BuildServiceProvider();
var logger = serviceProvider.GetService<ILoggerFactory>().CreateLogger<Program>();
//Now both are working
logger.LogDebug("Debug World");
logger.LogInformation("Hello World");
See also:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With