using HRD.AppLogger; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Hosting; namespace HRD.WebApi.Helpers { public static class ExceptionExtension { public static void AddCustomExceptionHandling(this IApplicationBuilder app) { app.UseExceptionHandler(errorApp => { errorApp.Run(async context => { context.Response.StatusCode = StatusCodes.Status500InternalServerError; context.Response.ContentType = "application/json"; var errorFeature = context.Features.Get(); if (errorFeature != null) { HttpErrorDetails customError = new HttpErrorDetails { URI = context.Request.Path.ToUriComponent(), Status = context.Response.StatusCode, Detail = errorFeature.Error.Message }; ILoggerManager logger = new LoggerManager(); logger.LogException(errorFeature.Error, null, customError.URI); await context.Response.WriteAsync(customError.ToJsonString()).ConfigureAwait(false); } }); }); } } }