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.
72 lines
2.0 KiB
C#
72 lines
2.0 KiB
C#
using Serilog;
|
|
using DocumentOperator.Infrastructure.Configuration;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// ========================================
|
|
// 1. Serilog Configuration
|
|
// ========================================
|
|
Log.Logger = new LoggerConfiguration()
|
|
.ReadFrom.Configuration(builder.Configuration)
|
|
.Enrich.FromLogContext()
|
|
.Enrich.WithProperty("Application", "DocumentOperator")
|
|
.CreateLogger();
|
|
|
|
builder.Host.UseSerilog();
|
|
|
|
Log.Information("Starting DocumentOperator API...");
|
|
|
|
try
|
|
{
|
|
// ========================================
|
|
// 2. Options Pattern Configuration
|
|
// ========================================
|
|
builder.Services.Configure<DocumentOperatorSettings>(
|
|
builder.Configuration.GetSection(DocumentOperatorSettings.SectionName));
|
|
|
|
builder.Services.Configure<RedisSettings>(
|
|
builder.Configuration.GetSection(RedisSettings.SectionName));
|
|
|
|
builder.Services.Configure<ApiKeySettings>(
|
|
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();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Fatal(ex, "Application startup failed");
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
Log.CloseAndFlush();
|
|
} |