C#C
C#3y 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();
        }
    }
}
GitHub
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...
Was this page helpful?