From bc0c7d319712923fb521a67e462349cf5c9a71fa Mon Sep 17 00:00:00 2001 From: David Headrick Date: Tue, 1 Jul 2025 16:21:31 -0500 Subject: [PATCH] Refactor configuration and error logging in Program.cs Removed custom configuration source and AdminContext creation. Added default service registration and SendGrid HTTP client. Updated error logging to use SurgeLoggerProvider for improved error handling during application startup. --- Surge365.MassEmailReact.API/Program.cs | 41 ++++---------------------- 1 file changed, 5 insertions(+), 36 deletions(-) diff --git a/Surge365.MassEmailReact.API/Program.cs b/Surge365.MassEmailReact.API/Program.cs index 0151ce1..05ea68f 100644 --- a/Surge365.MassEmailReact.API/Program.cs +++ b/Surge365.MassEmailReact.API/Program.cs @@ -17,8 +17,6 @@ using Surge365.MassEmailReact.Infrastructure.EntityMaps; var builder = WebApplication.CreateBuilder(args); -builder.AddCustomConfigurationSources(); - WebApplication? app = null; try { @@ -45,37 +43,8 @@ try }; }); - builder.Services.AddHttpContextAccessor(); - - Factory.RegisterDefaultServices(builder.Services, - adminContextProvider: provider => - { - var httpContextAccessor = provider.GetRequiredService(); - var httpContext = httpContextAccessor.HttpContext; - - if (httpContext == null) - return new AdminContext(); - - string? adminId = null; - string? ipAddress = httpContext?.Connection?.RemoteIpAddress?.ToString(); - string? appVersion = httpContext?.Request.Headers["app-version"].ToString(); - string? deviceInfo = httpContext?.Request.Headers["User-Agent"].ToString(); - - if (httpContext?.User?.Identity?.IsAuthenticated == true) - { - adminId = httpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? httpContext.User.FindFirst("sub")?.Value; - } - - return new AdminContext - { - LoggedIn = !string.IsNullOrWhiteSpace(adminId), - AdminId = adminId, - IpAddress = ipAddress, - AppVersion = appVersion, - DeviceInfo = deviceInfo - }; - } - ); + Factory.RegisterDefaultServices(builder.Services); + builder.Services.AddHttpClient("SendGridClient", client => { @@ -130,9 +99,9 @@ try } catch (Exception ex) { - Console.WriteLine($"Error during application startup: {ex.Message}"); - LoggingService appLoggingService = new LoggingService(builder.Configuration, new DataAccessFactory(builder.Configuration)); - appLoggingService.LogError(ex).Wait(); + SurgeLoggerProvider surgeLoggerProvider = new SurgeLoggerProvider(builder.Configuration); + ILogger surgeLogger = surgeLoggerProvider.CreateLogger(typeof(Program).ToString()); + surgeLogger.LogError(ex, "An error occurred during application startup."); return; } if (app != null)