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.
This commit is contained in:
David Headrick 2025-07-01 16:21:31 -05:00
parent 29e1f2fd62
commit bc0c7d3197

View File

@ -17,8 +17,6 @@ using Surge365.MassEmailReact.Infrastructure.EntityMaps;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.AddCustomConfigurationSources();
WebApplication? app = null; WebApplication? app = null;
try try
{ {
@ -45,37 +43,8 @@ try
}; };
}); });
builder.Services.AddHttpContextAccessor(); Factory.RegisterDefaultServices(builder.Services);
Factory.RegisterDefaultServices(builder.Services,
adminContextProvider: provider =>
{
var httpContextAccessor = provider.GetRequiredService<IHttpContextAccessor>();
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
};
}
);
builder.Services.AddHttpClient("SendGridClient", client => builder.Services.AddHttpClient("SendGridClient", client =>
{ {
@ -130,9 +99,9 @@ try
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Error during application startup: {ex.Message}"); SurgeLoggerProvider surgeLoggerProvider = new SurgeLoggerProvider(builder.Configuration);
LoggingService appLoggingService = new LoggingService(builder.Configuration, new DataAccessFactory(builder.Configuration)); ILogger surgeLogger = surgeLoggerProvider.CreateLogger(typeof(Program).ToString());
appLoggingService.LogError(ex).Wait(); surgeLogger.LogError(ex, "An error occurred during application startup.");
return; return;
} }
if (app != null) if (app != null)