From c0ea98f99b20cd603475673d82c28b87e1380d03 Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Sun, 17 May 2026 01:27:50 -0700 Subject: [PATCH] Refactor startup logging to use host logger Move the unsupported-profiler startup log to run after app build via DI ILogger, and remove the ad-hoc LoggerFactory creation/disposal so startup logging stays on the host-managed pipeline. --- EssentialCSharp.Web/Program.cs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/EssentialCSharp.Web/Program.cs b/EssentialCSharp.Web/Program.cs index f5c980ad..0b2021af 100644 --- a/EssentialCSharp.Web/Program.cs +++ b/EssentialCSharp.Web/Program.cs @@ -140,13 +140,6 @@ private static void Main(string[] args) ConfigurationManager configuration = builder.Configuration; string connectionString = builder.Configuration.GetConnectionString("EssentialCSharpWebContextConnection") ?? throw new InvalidOperationException("Connection string 'EssentialCSharpWebContextConnection' not found."); - // Create a logger that's accessible throughout the entire method - var loggerFactory = LoggerFactory.Create(loggingBuilder => - loggingBuilder.AddConsole().SetMinimumLevel(LogLevel.Information)); - var initialLogger = loggerFactory.CreateLogger(); - if (profilerSkippedUnsupportedPlatform) - LogSkippingUnsupportedAzureMonitorProfiler(initialLogger, RuntimeInformation.OSDescription); - builder.Services.AddDbContext(options => options.UseSqlServer(connectionString, sql => sql.EnableRetryOnFailure(5))); // Must be registered before AddDataProtection(): hosted services start in registration @@ -441,10 +434,13 @@ await context.HttpContext.Response.WriteAsync( }); } - loggerFactory.Dispose(); - WebApplication app = builder.Build(); + if (profilerSkippedUnsupportedPlatform) + LogSkippingUnsupportedAzureMonitorProfiler( + app.Services.GetRequiredService>(), + RuntimeInformation.OSDescription); + // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) {