Ignore HTTP errors based on status code
VoidMonk opened this issue · 6 comments
Hi, is it somehow possible to ignore HTTP errors with a status code below 500? I only want to log server errors which have a status code of 500 or above, or an empty status code.
You can custom filter anything here with events, but what are you actually trying to ignore here? Is it requests you're making to another server? Or errors from within your application?
It sounds like the former, in which case you're in complete control of the logging. Can you show how you're logging today? And are you using Exceptional v2?
Yes, I'm logging errors via Exceptional v2 from within a ASP.NET (non-Core) application, just using the default Web.config settings as shown at https://nickcraver.com/StackExchange.Exceptional/AspDotNet
I want to ignore HttpException errors based on their HTTP status code. I can't think of a robust regex for it to specify under <IgnoreErrors>
> <Regexes>
.
@VoidMonk Are you calling .Log()
on the error in a specific spot, or are they from all over?
@NickCraver I'm not calling .Log()
anywhere (for now). It's a default global handling (configured via Web.config) of uncaught exceptions & like.
Gotcha - in that case you'll want to hook into the OnBeforeLog
event for custom logic (you're handling a very specific type of exception) and set .Abort
on the args. For example, you could do this in your init code (e.g. Application_Start
):
Exceptional.Configure(settings =>
{
settings.OnBeforeLog += (sender, args) =>
{
if (args.Error.Exception is HttpException httpException && httpException.GetHttpCode() < 500)
{
args.Abort = true;
}
};
});
Handling specific bits of specific error types isn't something we can handle generically from Web.config (at least not in any reasonable way), so for this use case, go with a bit of programmatic configuration :)
Thanks @NickCraver. That's perfect for finer exception filtering.