Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HamidMosalla/6e31f2aaeb683ee773bdc8df40934357 to your computer and use it in GitHub Desktop.
Save HamidMosalla/6e31f2aaeb683ee773bdc8df40934357 to your computer and use it in GitHub Desktop.
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