Skip to content

Instantly share code, notes, and snippets.

@jamescrowley
Last active August 29, 2015 14:02
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 jamescrowley/fa05e279188dd5e4b9ef to your computer and use it in GitHub Desktop.
Save jamescrowley/fa05e279188dd5e4b9ef to your computer and use it in GitHub Desktop.
GlobalExceptionHandlingMiddleware
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);
}
}
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