An Azure service that provides an event-driven serverless compute platform.
yep - you can use ILogger after builder.Build() and before app.Run(). This allows startup checks to write to Application Insights, assuming its logging configuration is in place.
using Microsoft.Extensions.Logging;
var builder = FunctionsApplication.CreateBuilder(args);
builder.ConfigureFunctionsWebApplication();
builder.Services.AddApplicationInsightsTelemetryWorkerService();
builder.Services.ConfigureFunctionsApplicationInsights();
// Register services
var app = builder.Build();
var logger = app.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Starting configuration validation.");
try
{
var keyVaultUri = builder.Configuration["KeyVaultUri"];
if (string.IsNullOrWhiteSpace(keyVaultUri))
{
throw new InvalidOperationException(
"KeyVaultUri configuration is missing.");
}
logger.LogInformation("Key Vault configuration is present.");
// Perform actual Key Vault access validation here
logger.LogInformation("Key Vault successfully accessed.");
logger.LogInformation("Startup validation completed.");
}
catch (Exception ex)
{
logger.LogCritical(ex, "Startup validation failed.");
throw;
}
app.Run();
ILogger is preferable to Console.WriteLine() because it supports structured logging and can be routed to Application Insights.
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin