From 3764fdaf01134fe15b481453424f5d9bfc95a40a Mon Sep 17 00:00:00 2001 From: TekH Date: Fri, 5 Dec 2025 10:27:37 +0100 Subject: [PATCH] Enhance logging and refactor app startup - Integrated `NLog` for improved logging capabilities. - Added a logger instance and initialized logging with `NLog`. - Wrapped app setup in a `try-catch` block to handle startup exceptions. - Configured logging to use `NLog` in non-development environments. - Refactored service registration for better organization. - Retained key configurations for `AddRecServices` and `AddRecInfrastructure`. - Reorganized middleware pipeline setup for clarity. - Added exception logging during startup to improve debugging. --- src/ReC.API/Program.cs | 94 ++++++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 36 deletions(-) diff --git a/src/ReC.API/Program.cs b/src/ReC.API/Program.cs index 6b5cd81..fb9d327 100644 --- a/src/ReC.API/Program.cs +++ b/src/ReC.API/Program.cs @@ -1,56 +1,78 @@ using Microsoft.EntityFrameworkCore; +using NLog; +using NLog.Web; using ReC.API.Middleware; using ReC.Application; using ReC.Infrastructure; +using LogLevel = Microsoft.Extensions.Logging.LogLevel; -var builder = WebApplication.CreateBuilder(args); +var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger(); +logger.Info("Logging initialized!"); -var config = builder.Configuration; - -// Add services to the container. -builder.Services.AddRecServices(options => +try { - options.LuckyPennySoftwareLicenseKey = builder.Configuration["LuckyPennySoftwareLicenseKey"]; - options.ConfigureRecActions(config.GetSection("RecAction")); -}); + var builder = WebApplication.CreateBuilder(args); -builder.Services.AddRecInfrastructure(options => -{ - options.ConfigureDbContext((provider, opt) => + builder.Logging.SetMinimumLevel(LogLevel.Trace); + + if (!builder.Environment.IsDevelopment()) + { + builder.Logging.ClearProviders(); + builder.Host.UseNLog(); + } + + var config = builder.Configuration; + + // Add services to the container. + builder.Services.AddRecServices(options => { - var cnnStr = builder.Configuration.GetConnectionString("Default") - ?? throw new InvalidOperationException("Connection string is not found."); - - var logger = provider.GetRequiredService>(); - opt.UseSqlServer(cnnStr) - .LogTo(log => logger.LogInformation("{log}", log), LogLevel.Trace) - .EnableSensitiveDataLogging() - .EnableDetailedErrors(); + options.LuckyPennySoftwareLicenseKey = builder.Configuration["LuckyPennySoftwareLicenseKey"]; + options.ConfigureRecActions(config.GetSection("RecAction")); }); -}); -builder.Services.AddControllers(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); + builder.Services.AddRecInfrastructure(options => + { + options.ConfigureDbContext((provider, opt) => + { + var cnnStr = builder.Configuration.GetConnectionString("Default") + ?? throw new InvalidOperationException("Connection string is not found."); -var app = builder.Build(); + var logger = provider.GetRequiredService>(); + opt.UseSqlServer(cnnStr) + .LogTo(log => logger.LogInformation("{log}", log), LogLevel.Trace) + .EnableSensitiveDataLogging() + .EnableDetailedErrors(); + }); + }); + + builder.Services.AddControllers(); + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); + + var app = builder.Build(); #pragma warning disable CS0618 -app.UseMiddleware(); + app.UseMiddleware(); #pragma warning restore CS0618 -// Configure the HTTP request pipeline. -if (app.Environment.IsDevelopment()) -{ - app.UseSwagger(); - app.UseSwaggerUI(); -} + // Configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) + { + app.UseSwagger(); + app.UseSwaggerUI(); + } -app.UseHttpsRedirection(); + app.UseHttpsRedirection(); -app.UseAuthorization(); + app.UseAuthorization(); -app.MapControllers(); + app.MapControllers(); -app.Run(); + app.Run(); +} +catch(Exception ex) +{ + logger.Error(ex, "Stopped program because of exception"); + throw; +} \ No newline at end of file