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:
parent
5e7287bf86
commit
3764fdaf01
@ -1,10 +1,26 @@
|
||||
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 logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
|
||||
logger.Info("Logging initialized!");
|
||||
|
||||
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.
|
||||
@ -54,3 +70,9 @@ app.UseAuthorization();
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
logger.Error(ex, "Stopped program because of exception");
|
||||
throw;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user