Using the default WebAPI framework VS2022 in .NET 6. I would like to log information using ILogger, but before the call of "var app = builder.Build();
".
Is it possible to retrieve the logger newly configured by "buider.Host.ConfigureLogging(logging =>
" from the "WebApplication" object, or the WebApplicationBuilder object, or builder.Host object or any other object?
This is the code of program.cs
var builder = WebApplication.CreateBuilder(args);
string insightConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING");
builder.Host.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddApplicationInsights(insightConnectionString);
logging.AddConsole();
});
// -----> I want to log information here, before the call of "var app = builder.Build();" <-----
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// -----> And I want to log information here, before the call of "var app = builder.Build();" <-----
var app = builder.Build();
app.Logger.LogInformation("Log some information #1");
//...
app.Logger.LogInformation("Log some information #2");
app.Run();