Skip to content

Instantly share code, notes, and snippets.

Last active Nov 8, 2016
What would you like to do?
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);

This comment has been minimized.

Copy link

@ctrl-brk 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?


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