first commit
This commit is contained in:
75
MyNewProjectName.WebAPI/Program.cs
Normal file
75
MyNewProjectName.WebAPI/Program.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using MyNewProjectName.Application;
|
||||
using MyNewProjectName.Infrastructure;
|
||||
using MyNewProjectName.Infrastructure.Extensions;
|
||||
using MyNewProjectName.WebAPI.Middleware;
|
||||
using Serilog;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Configure Serilog
|
||||
builder.Host.UseSerilogLogging(builder.Configuration);
|
||||
|
||||
// Add OpenTelemetry distributed tracing
|
||||
builder.Services.AddOpenTelemetryTracing("MyNewProjectName.WebAPI");
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddControllers();
|
||||
|
||||
// Add Application Layer
|
||||
builder.Services.AddApplication();
|
||||
|
||||
// Add Infrastructure Layer
|
||||
builder.Services.AddInfrastructure(builder.Configuration);
|
||||
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
// Add Swagger
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
// Middleware pipeline order is critical:
|
||||
// 1. CorrelationIdMiddleware - Must be first to track all requests
|
||||
app.UseMiddleware<CorrelationIdMiddleware>();
|
||||
|
||||
// 2. RequestResponseLoggingMiddleware - Optional, enable only when needed
|
||||
// WARNING: This can generate large log files. Enable only for specific environments.
|
||||
// Configure in appsettings.json: "Logging:EnableRequestLogging" and "Logging:EnableResponseLogging"
|
||||
app.UseMiddleware<RequestResponseLoggingMiddleware>();
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
// 3. Authentication (built-in)
|
||||
app.UseAuthentication();
|
||||
|
||||
// 4. Authorization (built-in)
|
||||
app.UseAuthorization();
|
||||
|
||||
// 6. ExceptionHandlingMiddleware - Must be last to catch all exceptions
|
||||
app.UseMiddleware<ExceptionHandlingMiddleware>();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
try
|
||||
{
|
||||
Log.Information("Starting MyNewProjectName.WebAPI");
|
||||
app.Run();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Fatal(ex, "Application terminated unexpectedly");
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user