示例#1
0
 public async Task InvokeAsync(HttpContext httpContext)
 {
     try
     {
         await _NextDelegate(httpContext);
     }
     catch (Exception ex)
     {
         _LoggerUtility.LogError($"Something went wrong.{Environment.NewLine}Details: {ex}");
         await HandleExceptionAsync(httpContext, ex);
     }
 }
示例#2
0
        public static void ConfigureExceptionHandler(this IApplicationBuilder appExHandler, ILoggerManagerUtility loggerUtitlity)
        {
            // Extension method that registers the UseExceptionHandler middleware
            appExHandler.UseExceptionHandler(appError =>
            {
                appError.Run(async context =>
                {
                    context.Response.StatusCode  = (int)HttpStatusCode.InternalServerError;
                    context.Response.ContentType = "application/json";

                    var contextFeature = context.Features.Get <IExceptionHandlerFeature>();
                    if (contextFeature != null)
                    {
                        loggerUtitlity.LogError($"Something went wrong.{Environment.NewLine}Details: {contextFeature.Error}");

                        await context.Response.WriteAsync(new ErrorDetails()
                        {
                            StatusCode = context.Response.StatusCode,
                            Message    = "Internal Server Error."
                        }.ToString());
                    }
                });
            });
        }