@@ -42,8 +42,8 @@ private static void Main(string[] args)
4242 builder . Logging . AddConsole ( ) ;
4343 builder . Services . AddHealthChecks ( ) ;
4444
45- // Create a temporary logger for startup logging
46- using var loggerFactory = LoggerFactory . Create ( loggingBuilder =>
45+ // Create a logger that's accessible throughout the entire method
46+ var loggerFactory = LoggerFactory . Create ( loggingBuilder =>
4747 loggingBuilder . AddConsole ( ) . SetMinimumLevel ( LogLevel . Information ) ) ;
4848 var initialLogger = loggerFactory . CreateLogger < Program > ( ) ;
4949
@@ -225,7 +225,7 @@ await context.HttpContext.Response.WriteAsync(
225225 cancellationToken ) ;
226226
227227 // Optional logging
228- logger . LogWarning ( "Rate limit exceeded for user: {User}, IP: {IpAddress}" ,
228+ initialLogger . LogWarning ( "Rate limit exceeded for user: {User}, IP: {IpAddress}" ,
229229 context . HttpContext . User . Identity ? . Name ?? "anonymous" ,
230230 context . HttpContext . Connection . RemoteIpAddress ) ;
231231 return ;
@@ -234,7 +234,7 @@ await context.HttpContext.Response.WriteAsync(
234234 await context . HttpContext . Response . WriteAsync ( "Rate limit exceeded. Please try again later." , cancellationToken ) ;
235235
236236 // Optional logging
237- logger . LogWarning ( "Rate limit exceeded for user: {User}, IP: {IpAddress}" ,
237+ initialLogger . LogWarning ( "Rate limit exceeded for user: {User}, IP: {IpAddress}" ,
238238 context . HttpContext . User . Identity ? . Name ?? "anonymous" ,
239239 context . HttpContext . Connection . RemoteIpAddress ) ;
240240 } ;
@@ -270,7 +270,7 @@ await context.HttpContext.Response.WriteAsync(
270270 } ) ;
271271 }
272272
273-
273+ loggerFactory . Dispose ( ) ;
274274
275275 WebApplication app = builder . Build ( ) ;
276276 // Configure the HTTP request pipeline.
@@ -321,7 +321,7 @@ await context.HttpContext.Response.WriteAsync(
321321 var routeConfigurationService = app . Services . GetRequiredService < IRouteConfigurationService > ( ) ;
322322
323323 SitemapXmlHelpers . EnsureSitemapHealthy ( siteMappingService . SiteMappings . ToList ( ) ) ;
324- SitemapXmlHelpers . GenerateAndSerializeSitemapXml ( wwwrootDirectory , siteMappingService . SiteMappings . ToList ( ) , logger , routeConfigurationService , baseUrl ) ;
324+ SitemapXmlHelpers . GenerateAndSerializeSitemapXml ( wwwrootDirectory , siteMappingService . SiteMappings . ToList ( ) , initialLogger , routeConfigurationService , baseUrl ) ;
325325 logger . LogInformation ( "Sitemap.xml generation completed successfully during application startup" ) ;
326326 }
327327 catch ( Exception ex )
0 commit comments