Home > Net >  Console Logging in .NET Core 6?
Console Logging in .NET Core 6?

Time:05-06

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?

Program.cs

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

CodePudding user response:

You need to add the following to log Debug:

builder.SetMinimumLevel(LogLevel.Debug);
  • Related