-
-
Save justinyoo/4c3526deee32f9358557 to your computer and use it in GitHub Desktop.
ASP.NET Core Tips & Tricks
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 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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tried to just copy/paste and use but there're couple issues.
Thanks.