with your project set to C# 10 (add <LangVersion>preview</LangVersion>
to your .csproj file), in your Program.cs simply add ILogger<Program> logger
to your endpoint delegate:
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace MinimalWebApiWithLogging
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/hello/{name}", (ILogger<Program> logger, string name) =>
{
logger.LogInformation("say hello to {name} ...", name);
return Results.Text($"hello {name}");
});
app.Logger.LogInformation("starting the app ...");
app.Run();
}
}
}
... yes, C# 10 allows you to remove all boiler plate code, so only lines 18 - 28 will remain ;-)