I am learning how to use the built-in .net core logging from Microsoft.Extensions.*. I found a strange output as follows. LogTrace and LogDebug don't produce any output on the output window. What causes this issue?
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
class Foo
{
static void Main(string[] args)
{
Console.WriteLine("Begin...");
IServiceCollection sc = new ServiceCollection()
.AddLogging();
IServiceProvider isp = sc.BuildServiceProvider();
using (ILoggerFactory ilf = isp.GetRequiredService<ILoggerFactory>())
{
ilf.AddConsole() ;
ILogger logger = ilf.CreateLogger<Program>();
logger.LogTrace("Trace...");
logger.LogDebug("Debug...");
logger.LogInformation("Information...");
logger.LogWarning("Warning...");
logger.LogError("Error...");
logger.LogCritical("Critical...");
}
Console.WriteLine("End...");
}
}
Is Log filtering enabled?
It's not a bug, it's not read docs :wink:
For your particular case, here's the subsection that is applicable:
If you don't explicitly set the minimum level, the default value is Information, which means that Trace and Debug logs are ignored.
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.2#default-minimum-level
To configure it to show trace and debug, assuming you're using v2.1.1 (since ILoggerFactory.AddConsole() was deprecated in v2.2), you'd have to add both of the calls that have LogLevel.Trace specified:
``` C#
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Begin...");
IServiceCollection sc = new ServiceCollection()
.AddLogging(l => l.SetMinimumLevel(LogLevel.Trace)); // <==
IServiceProvider isp = sc.BuildServiceProvider();
using (ILoggerFactory ilf = isp.GetRequiredService<ILoggerFactory>())
{
ilf.AddConsole(LogLevel.Trace); // <==
ILogger logger = ilf.CreateLogger<Program>();
logger.LogTrace("Trace...\r\n");
logger.LogDebug("Debug...\r\n");
logger.LogInformation("Information...\r\n");
logger.LogWarning("Warning...\r\n");
logger.LogError("Error...\r\n");
logger.LogCritical("Critical...\r\n");
}
Console.WriteLine("End...");
}
}
In 3.0, you'd have this code instead:
``` C#
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Begin...");
IServiceCollection sc = new ServiceCollection()
.AddLogging(l => l
.AddConsole()
.SetMinimumLevel(LogLevel.Trace));
IServiceProvider isp = sc.BuildServiceProvider();
// No need to get a ILoggerFactory
ILogger logger = isp.GetRequiredService<ILogger<Program>>();
logger.LogTrace("Trace...");
logger.LogDebug("Debug...");
logger.LogInformation("Information...");
logger.LogWarning("Warning...");
logger.LogError("Error...");
logger.LogCritical("Critical...");
// Console logger is now queued on a background thread
Thread.Sleep(1000);
Console.WriteLine("End...");
}
}
@brandondahler Thank you very much for your detailed answer. This kind of response is extremely understandable and useful.
Most helpful comment
@brandondahler Thank you very much for your detailed answer. This kind of response is extremely understandable and useful.