Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Last active November 8, 2016 23:16
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 justinyoo/4c3526deee32f9358557 to your computer and use it in GitHub Desktop.
Save justinyoo/4c3526deee32f9358557 to your computer and use it in GitHub Desktop.
ASP.NET Core Tips & Tricks
public class GlobalExceptionFilter : IExceptionFilter, IDisposable
{
private readonly ILogger _logger;
public GlobalExceptionFilter(ILoggerFactory logger)
{
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
this._logger = logger.CreateLogger("Global Exception Filter");
}
public void OnException(ExceptionContext context)
{
var response = new ErrorResponse()
{
Message = context.Exception.Message,
StackTrace = context.Exception.StackTrace
};
context.Result = new ObjectResult(response)
{
StatusCode = 500,
DeclaredType = typeof(ErrorResponse)
};
this._logger.LogError("GlobalExceptionFilter", context.Exception);
}
}
@ctrl-brk
Copy link

ctrl-brk commented Nov 8, 2016

Tried to just copy/paste and use but there're couple issues.

  1. Dispose() is not implemented
  2. Where does ErrorResponse come from?

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment