C
C#2y ago
voltagex

❔ ILogger is never enabled

Any idea what's going on here? https://github.com/dotnet/runtime/issues/80336
using Microsoft.AspNetCore.Mvc;

namespace TestWebApp
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddAuthorization();

builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.SetMinimumLevel(LogLevel.Trace);
var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseAuthorization();

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", (HttpContext httpContext, [FromServices] ILogger<Program> logger) =>
{
logger.LogTrace("Trace event from /weatherforecast");
Console.WriteLine($"ILogger enabled for trace? {logger.IsEnabled(LogLevel.Trace)}");
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
})
.ToArray();
return forecast;
});

app.Run();
}
}
}
using Microsoft.AspNetCore.Mvc;

namespace TestWebApp
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddAuthorization();

builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.SetMinimumLevel(LogLevel.Trace);
var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseAuthorization();

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", (HttpContext httpContext, [FromServices] ILogger<Program> logger) =>
{
logger.LogTrace("Trace event from /weatherforecast");
Console.WriteLine($"ILogger enabled for trace? {logger.IsEnabled(LogLevel.Trace)}");
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
})
.ToArray();
return forecast;
});

app.Run();
}
}
}
GitHub
WebApplicationBuilder ILoggingBuilder does not enable logging by de...
Description Seems to be similar to #79798, but I have a repro with .NET 7 - either the Web API template needs to be changed or the documentation for logging needs to be clarified or both. Reproduct...
2 Replies
undisputed world champions
i'd guess builder.Logging.ClearProviders(); removes all providers including the build in extension.logging one
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.