防止将特定异常记录到 App Insights

Pau*_*aul 3 c# exception azure azure-application-insights appinsights

这是一个很遥远的事情,谷歌上似乎没有任何接近的东西,但我想知道是否有可能阻止某些异常被记录到 App Insights 中?

我们的代码库中有很多地方针对用户操作引发了异常,但这些异常并不是我们想要在 App Insights 中显示的真正异常。

然而,改变发生这种情况的所有区域将需要很长时间,并且需要大量测试,因为我们需要确保逻辑流程不改变

目前我们引发了 LocationDomainException 类型的异常,我想如果我遇到不想记录异常的地方,我们可以更改为 LocationDomainUserError

我需要停止记录此异常或从其继承的任何异常

我使用 .NET Core 3.1。

我知道这有点反模式,但有人尝试过吗?

Pet*_*ons 11

是的,您可以使用遥测处理器

public class CustomTelemetryFilter : ITelemetryProcessor
    {
        private readonly ITelemetryProcessor _next;

        public CustomTelemetryFilter(ITelemetryProcessor next)
        {
            _next = next;
        }

        public void Process(ITelemetry item)
        {
            // Example: process all exceptions except LocationDomainException
            var isSomeException = item is ExceptionTelemetry ex && ex.Exception is LocationDomainException;

            if (!isSomeException)
                _next.Process(item); // Process the item
            else
            {
                // Item is dropped here
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

使用注册处理器services.AddApplicationInsightsTelemetryProcessor<CustomTelemetryFilter>();