From d8f3143c8ad79f63f546db6fa346c895e26e217a Mon Sep 17 00:00:00 2001 From: OlgunR Date: Tue, 16 Jun 2026 11:21:03 +0200 Subject: [PATCH] Integrate Serilog and add configuration classes Enhanced logging with Serilog, including request logging and structured exception handling during startup. Added support for the Options Pattern with new configuration classes: `DocumentOperatorSettings`, `RedisSettings`, and `ApiKeySettings`. Introduced `TenantInfo` class for tenant management. Updated project files to include new dependencies and removed unused `Configuration` folder reference. --- .../DocumentOperator.API.csproj | 4 +- DocumentOperator.API/Program.cs | 83 +++++++++++++++---- .../Configuration/ApiKeySettings.cs | 9 ++ .../Configuration/DocumentOperatorSettings.cs | 11 +++ .../Configuration/RedisSettings.cs | 10 +++ .../Configuration/TenantInfo.cs | 8 ++ .../DocumentOperator.Infrastructure.csproj | 1 - 7 files changed, 106 insertions(+), 20 deletions(-) create mode 100644 DocumentOperator.Infrastructure/Configuration/ApiKeySettings.cs create mode 100644 DocumentOperator.Infrastructure/Configuration/DocumentOperatorSettings.cs create mode 100644 DocumentOperator.Infrastructure/Configuration/RedisSettings.cs create mode 100644 DocumentOperator.Infrastructure/Configuration/TenantInfo.cs diff --git a/DocumentOperator.API/DocumentOperator.API.csproj b/DocumentOperator.API/DocumentOperator.API.csproj index 7b15771..83c30fc 100644 --- a/DocumentOperator.API/DocumentOperator.API.csproj +++ b/DocumentOperator.API/DocumentOperator.API.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -10,6 +10,8 @@ + + diff --git a/DocumentOperator.API/Program.cs b/DocumentOperator.API/Program.cs index 48863a6..308e73a 100644 --- a/DocumentOperator.API/Program.cs +++ b/DocumentOperator.API/Program.cs @@ -1,25 +1,72 @@ +using Serilog; +using DocumentOperator.Infrastructure.Configuration; + var builder = WebApplication.CreateBuilder(args); -// Add services to the container. +// ======================================== +// 1. Serilog Configuration +// ======================================== +Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(builder.Configuration) + .Enrich.FromLogContext() + .Enrich.WithProperty("Application", "DocumentOperator") + .CreateLogger(); -builder.Services.AddControllers(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +builder.Host.UseSerilog(); -var app = builder.Build(); +Log.Information("Starting DocumentOperator API..."); -// Configure the HTTP request pipeline. -if (app.Environment.IsDevelopment()) +try { - app.UseSwagger(); - app.UseSwaggerUI(); + // ======================================== + // 2. Options Pattern Configuration + // ======================================== + builder.Services.Configure( + builder.Configuration.GetSection(DocumentOperatorSettings.SectionName)); + + builder.Services.Configure( + builder.Configuration.GetSection(RedisSettings.SectionName)); + + builder.Services.Configure( + builder.Configuration.GetSection(ApiKeySettings.SectionName)); + + // ======================================== + // 3. Services + // ======================================== + builder.Services.AddControllers(); + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); + + // ======================================== + // 4. Build App + // ======================================== + var app = builder.Build(); + + // ======================================== + // 5. Middleware Pipeline + // ======================================== + if (app.Environment.IsDevelopment()) + { + app.UseSwagger(); + app.UseSwaggerUI(); + } + + app.UseSerilogRequestLogging(); // Log HTTP Requests + + app.UseHttpsRedirection(); + app.UseAuthorization(); + app.MapControllers(); + + Log.Information("DocumentOperator API started successfully"); + + app.Run(); } - -app.UseHttpsRedirection(); - -app.UseAuthorization(); - -app.MapControllers(); - -app.Run(); +catch (Exception ex) +{ + Log.Fatal(ex, "Application startup failed"); + throw; +} +finally +{ + Log.CloseAndFlush(); +} \ No newline at end of file diff --git a/DocumentOperator.Infrastructure/Configuration/ApiKeySettings.cs b/DocumentOperator.Infrastructure/Configuration/ApiKeySettings.cs new file mode 100644 index 0000000..0c6ad7b --- /dev/null +++ b/DocumentOperator.Infrastructure/Configuration/ApiKeySettings.cs @@ -0,0 +1,9 @@ +namespace DocumentOperator.Infrastructure.Configuration; + +public class ApiKeySettings +{ + public const string SectionName = "ApiKeySettings"; + + public bool EnableValidation { get; set; } = true; + public Dictionary Keys { get; set; } = new(); +} diff --git a/DocumentOperator.Infrastructure/Configuration/DocumentOperatorSettings.cs b/DocumentOperator.Infrastructure/Configuration/DocumentOperatorSettings.cs new file mode 100644 index 0000000..8f5811a --- /dev/null +++ b/DocumentOperator.Infrastructure/Configuration/DocumentOperatorSettings.cs @@ -0,0 +1,11 @@ +namespace DocumentOperator.Infrastructure.Configuration; + +public class DocumentOperatorSettings +{ + public const string SectionName = "DocumentOperatorSettings"; + + public string TempFolderPath { get; set; } = string.Empty; + public int TempFileRetentionHours { get; set; } + public int MaxPdfSizeMB { get; set; } + public bool EnableDetailedLogging { get; set; } +} \ No newline at end of file diff --git a/DocumentOperator.Infrastructure/Configuration/RedisSettings.cs b/DocumentOperator.Infrastructure/Configuration/RedisSettings.cs new file mode 100644 index 0000000..407ef5f --- /dev/null +++ b/DocumentOperator.Infrastructure/Configuration/RedisSettings.cs @@ -0,0 +1,10 @@ +namespace DocumentOperator.Infrastructure.Configuration; + +public class RedisSettings +{ + public const string SectionName = "RedisSettings"; + + public string ConnectionString { get; set; } = "localhost:6379"; + public string InstanceName { get; set; } = "DocumentOperator:"; + public int CacheExpirationMinutes { get; set; } = 60; +} \ No newline at end of file diff --git a/DocumentOperator.Infrastructure/Configuration/TenantInfo.cs b/DocumentOperator.Infrastructure/Configuration/TenantInfo.cs new file mode 100644 index 0000000..c10c106 --- /dev/null +++ b/DocumentOperator.Infrastructure/Configuration/TenantInfo.cs @@ -0,0 +1,8 @@ +namespace DocumentOperator.Infrastructure.Configuration; + +public class TenantInfo +{ + public string TenantId { get; set; } = string.Empty; + public string TenantName { get; set; } = string.Empty; + public bool IsActive { get; set; } = true; +} diff --git a/DocumentOperator.Infrastructure/DocumentOperator.Infrastructure.csproj b/DocumentOperator.Infrastructure/DocumentOperator.Infrastructure.csproj index 8c8d9d4..717e2d5 100644 --- a/DocumentOperator.Infrastructure/DocumentOperator.Infrastructure.csproj +++ b/DocumentOperator.Infrastructure/DocumentOperator.Infrastructure.csproj @@ -17,7 +17,6 @@ -