NickCraver / StackExchange.Exceptional

Error handler used for the Stack Exchange network
https://nickcraver.com/StackExchange.Exceptional/
Apache License 2.0
859 stars 170 forks source link

Ignore HTTP errors based on status code #167

Closed VoidMonk closed 5 years ago

VoidMonk commented 5 years ago

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.

NickCraver commented 5 years ago

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?

VoidMonk commented 5 years ago

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>.

NickCraver commented 5 years ago

@voidmonk Are you calling .Log() on the error in a specific spot, or are they from all over?

VoidMonk commented 5 years ago

@NickCraver I'm not calling .Log() anywhere (for now). It's a default global handling (configured via Web.config) of uncaught exceptions & like.

NickCraver commented 5 years ago

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 :)

VoidMonk commented 5 years ago

Thanks @NickCraver. That's perfect for finer exception filtering.