Structured logging
As stated at the beginning, structured logging can be a game changer that opens opportunities. Querying a data structure is always more versatile than querying a single line of text. That is even more true if there is no clear guideline around logging, whether a line of text or a JSON-formatted data structure.
To keep it simple, we leverage a built-in formatter (highlighted line below) that serializes our log entries into JSON. Here is the Program.cs
file:
var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddJsonConsole();
var app = builder.Build();
app.MapGet("/", (ILoggerFactory loggerFactory) =>
{
const string category = "root";
var logger = loggerFactory.CreateLogger(category);
logger.LogInformation("You hit the {category} URL!", category);
return "Hello World!";
});
app.Run();
The preceding code transforms the console to logging JSON. For example, every time we hit the ...