Add Exception middleware
This commit is contained in:
121
Program.cs
121
Program.cs
@@ -1,20 +1,26 @@
|
||||
using Diary.Data;
|
||||
using Diary.Installers;
|
||||
using Diary.Shared;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
using System.Reflection;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Settings - Done by sefaulr
|
||||
* Logging - Done
|
||||
* Database - Donw
|
||||
* Dependencies
|
||||
* automapper - Done
|
||||
* Automapper - Done
|
||||
* Cors
|
||||
* Views/Filters/Validation
|
||||
* Swagger - Done
|
||||
* Auth
|
||||
*
|
||||
* Exception Middleware - Done
|
||||
* Validation Filter
|
||||
* */
|
||||
|
||||
//+Setup Logger
|
||||
@@ -28,62 +34,65 @@ Log.Logger = new LoggerConfiguration()
|
||||
.Enrich.FromLogContext()
|
||||
.CreateLogger();
|
||||
|
||||
Log.Information("Starting up");
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
//---Y
|
||||
//Add Serilog
|
||||
builder.Host.UseSerilog(Log.Logger);
|
||||
builder.Services.AddSingleton<IHostedService>(new LoggingStartService(Log.Logger, Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().ManifestModule.ScopeName)));
|
||||
|
||||
//---Y
|
||||
//Database
|
||||
builder.Services.AddDatabase(builder.Configuration);
|
||||
builder.Services.AddScoped<IDiaryDBContext, DiaryDBContext>();
|
||||
|
||||
//---y
|
||||
//Dependancies
|
||||
builder.Services.AddDependencies();
|
||||
|
||||
//---Y
|
||||
//Automapper
|
||||
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
builder.Services.AddExceptionsMiddleware();
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
//---R
|
||||
//+Build
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(opt =>
|
||||
{
|
||||
opt.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
|
||||
opt.RoutePrefix = String.Empty;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
app.UseSerilogRequestLogging();
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.UseMiddleware<ExceptionHandlingMiddleware>();
|
||||
|
||||
try
|
||||
{
|
||||
Log.Information("Starting up");
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
//---Y
|
||||
//Add Serilog
|
||||
builder.Host.UseSerilog(Log.Logger);
|
||||
builder.Services.AddSingleton<IHostedService>(new LoggingStartService(Log.Logger, Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().ManifestModule.ScopeName)));
|
||||
|
||||
//---Y
|
||||
//Database
|
||||
builder.Services.AddDatabase(builder.Configuration);
|
||||
builder.Services.AddScoped<IDiaryDBContext, DiaryDBContext>();
|
||||
|
||||
//---y
|
||||
//Dependancies
|
||||
builder.Services.AddDependencies();
|
||||
|
||||
|
||||
//---Y
|
||||
//Automapper
|
||||
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
//---R
|
||||
//+Build
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(opt =>
|
||||
{
|
||||
opt.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
|
||||
opt.RoutePrefix = String.Empty;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
app.UseSerilogRequestLogging();
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
Reference in New Issue
Block a user