Created
February 15, 2018 07:27
-
-
Save HamidMosalla/6e31f2aaeb683ee773bdc8df40934357 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class AspNetCoreGlobalExceptionHandlerExtension | |
{ | |
public static IApplicationBuilder UseAspNetCoreExceptionHandler(this IApplicationBuilder app) | |
{ | |
var loggerFactory = app.ApplicationServices.GetService(typeof(ILoggerFactory)) as ILoggerFactory; | |
return app.UseExceptionHandler(HandleException(loggerFactory)); | |
} | |
public static Action<IApplicationBuilder> HandleException(ILoggerFactory loggerFactory) | |
{ | |
return appBuilder => | |
{ | |
appBuilder.Run(async context => | |
{ | |
var exceptionHandlerFeature = context.Features.Get<IExceptionHandlerFeature>(); | |
if (exceptionHandlerFeature != null) | |
{ | |
var logger = loggerFactory.CreateLogger("Serilog Global exception logger"); | |
logger.LogError(500, exceptionHandlerFeature.Error, exceptionHandlerFeature.Error.Message); | |
} | |
context.Response.Redirect($"/Error/Status/{context.Response.StatusCode}"); | |
}); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment