/// <summary> /// Adds a middleware to the pipeline that will catch exceptions, log them, reset the request path, and re-execute the request. /// The request will not be re-executed if the response has already started. /// </summary> /// <param name="app"></param> /// <param name="errorHandlingPath"></param> /// <returns></returns> public static IApplicationBuilder UseExceptionHandler(this IApplicationBuilder app, string errorHandlingPath) { var options = new ExceptionHandlerOptions() { ExceptionHandlingPath = new PathString(errorHandlingPath) }; return app.UseMiddleware<ExceptionHandlerMiddleware>(options); }
/// <summary> /// Adds a middleware to the pipeline that will catch exceptions, log them, and re-execute the request in an alternate pipeline. /// The request will not be re-executed if the response has already started. /// </summary> /// <param name="app"></param> /// <param name="configure"></param> /// <returns></returns> public static IApplicationBuilder UseExceptionHandler(this IApplicationBuilder app, Action<IApplicationBuilder> configure) { var subAppBuilder = app.New(); configure(subAppBuilder); var exceptionHandlerPipeline = subAppBuilder.Build(); var options = new ExceptionHandlerOptions() { ExceptionHandler = exceptionHandlerPipeline }; return app.UseMiddleware<ExceptionHandlerMiddleware>(options); }
public ExceptionHandlerMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, ExceptionHandlerOptions options) { _next = next; _options = options; _logger = loggerFactory.CreateLogger<ExceptionHandlerMiddleware>(); if (_options.ExceptionHandler == null) { _options.ExceptionHandler = _next; } _clearCacheHeadersDelegate = ClearCacheHeaders; }