Last active
August 29, 2015 14:02
-
-
Save jamescrowley/fa05e279188dd5e4b9ef to your computer and use it in GitHub Desktop.
GlobalExceptionHandlingMiddleware
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 class GlobalExceptionHandlingMiddleware : OwinMiddleware | |
{ | |
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); | |
public GlobalExceptionHandlingMiddleware(OwinMiddleware next) : base(next) | |
{ | |
} | |
public override async Task Invoke(IOwinContext context) | |
{ | |
try | |
{ | |
await Next.Invoke(context); | |
} | |
catch (Exception ex) | |
{ | |
OnError(context, ex.InnerException ?? ex); | |
} | |
} | |
static void OnError(IOwinContext context, Exception ex) | |
{ | |
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; | |
LogException(ex); | |
} | |
static void LogException(Exception exception) | |
{ | |
Logger.Error(exception.Message, exception); | |
} | |
} |
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 class Startup | |
{ | |
public void Configuration(IAppBuilder app) | |
{ | |
app | |
.UseCustomErrorPages() | |
.Use<GlobalExceptionHandlingMiddleware>() | |
.Use<SecurityBestPracticeMiddleware>() | |
.UseNancy(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment