NickCraver / StackExchange.Exceptional

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

Is there a way to filter logs by Client IP? Want to prevent logging at all from certain IP addresses #185

Closed kbdavis07 closed 3 years ago

kbdavis07 commented 4 years ago

Is there a way to filter logs by Client IP? Want to prevent logging at all from certain IP addresses.

Don't want to save logs from certain IP addresses in the database at all.

NickCraver commented 4 years ago

There's no way do to this via a built-in filter, because the concept is a lot more complicated than most devs imagine depending on context. "What is the user's IP?" gets really weird when behind a load balancer or DDoS protection layer (e.g. Fastly or CloudFlare) and...yeah. It's all weird.

But, you can likely do what you want in code in OnBeforeLog by setting Abort = true based on what you want. Here's an ASP.NET Core simple example that I hope helps!

services.AddExceptional(Configuration.GetSection("Exceptional"), settings =>
{
    settings.OnBeforeLog += (sender, args) =>
    {
        // Don't log local errors!
        if (args.Error.IPAddress == "127.0.0.1")
        {
            args.Abort = true;
        }
    };
});
NickCraver commented 3 years ago

Closing this out to cleanup - approach is above!