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.
This commit is contained in:
tekh 2025-12-05 10:27:37 +01:00
parent 5e7287bf86
commit 3764fdaf01

View File

@ -1,21 +1,37 @@
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
{
var builder = WebApplication.CreateBuilder(args);
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 =>
{
options.LuckyPennySoftwareLicenseKey = builder.Configuration["LuckyPennySoftwareLicenseKey"];
options.ConfigureRecActions(config.GetSection("RecAction"));
});
});
builder.Services.AddRecInfrastructure(options =>
{
builder.Services.AddRecInfrastructure(options =>
{
options.ConfigureDbContext((provider, opt) =>
{
var cnnStr = builder.Configuration.GetConnectionString("Default")
@ -27,30 +43,36 @@ builder.Services.AddRecInfrastructure(options =>
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
});
});
});
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
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();
var app = builder.Build();
#pragma warning disable CS0618
app.UseMiddleware<ExceptionHandlingMiddleware>();
app.UseMiddleware<ExceptionHandlingMiddleware>();
#pragma warning restore CS0618
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
catch(Exception ex)
{
logger.Error(ex, "Stopped program because of exception");
throw;
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();