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,56 +1,78 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NLog;
using NLog.Web;
using ReC.API.Middleware; using ReC.API.Middleware;
using ReC.Application; using ReC.Application;
using ReC.Infrastructure; 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; try
// Add services to the container.
builder.Services.AddRecServices(options =>
{ {
options.LuckyPennySoftwareLicenseKey = builder.Configuration["LuckyPennySoftwareLicenseKey"]; var builder = WebApplication.CreateBuilder(args);
options.ConfigureRecActions(config.GetSection("RecAction"));
});
builder.Services.AddRecInfrastructure(options => builder.Logging.SetMinimumLevel(LogLevel.Trace);
{
options.ConfigureDbContext((provider, opt) => if (!builder.Environment.IsDevelopment())
{ {
var cnnStr = builder.Configuration.GetConnectionString("Default") builder.Logging.ClearProviders();
?? throw new InvalidOperationException("Connection string is not found."); builder.Host.UseNLog();
}
var logger = provider.GetRequiredService<ILogger<RecDbContext>>(); var config = builder.Configuration;
opt.UseSqlServer(cnnStr)
.LogTo(log => logger.LogInformation("{log}", log), LogLevel.Trace) // Add services to the container.
.EnableSensitiveDataLogging() builder.Services.AddRecServices(options =>
.EnableDetailedErrors(); {
options.LuckyPennySoftwareLicenseKey = builder.Configuration["LuckyPennySoftwareLicenseKey"];
options.ConfigureRecActions(config.GetSection("RecAction"));
}); });
});
builder.Services.AddControllers(); builder.Services.AddRecInfrastructure(options =>
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle {
builder.Services.AddEndpointsApiExplorer(); options.ConfigureDbContext((provider, opt) =>
builder.Services.AddSwaggerGen(); {
var cnnStr = builder.Configuration.GetConnectionString("Default")
?? throw new InvalidOperationException("Connection string is not found.");
var app = builder.Build(); var logger = provider.GetRequiredService<ILogger<RecDbContext>>();
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 #pragma warning disable CS0618
app.UseMiddleware<ExceptionHandlingMiddleware>(); app.UseMiddleware<ExceptionHandlingMiddleware>();
#pragma warning restore CS0618 #pragma warning restore CS0618
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(); app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
} }
catch(Exception ex)
app.UseHttpsRedirection(); {
logger.Error(ex, "Stopped program because of exception");
app.UseAuthorization(); throw;
}
app.MapControllers();
app.Run();