c# - 使用 Application Insights 抑制 "The controller for path '/' was not found or does not implement IController."服务器端

标签 c# azure-application-insights

我知道这个讨论(没有异常(exception)的源端解决方案):https://social.msdn.microsoft.com/Forums/vstudio/en-US/cb944fc4-4410-4e49-ba0b-43e675fe2173/ignoring-certain-exeptions-not-implementing-icontroller-bad-url?forum=ApplicationInsights

我有同样的问题,因为我不想用机器人生成的流量污染我的异常日志(我们只有 Web API Controller )。我能够使用遥测处理器忽略那些服务器请求(通常显示在“失败的请求”中)并简单地返回 404。

但是,无法摆脱异常条目 - 有没有人知道设置/过滤器/你的名字 - 它可以让我决定服务器端(在它们被发送之前)哪些异常是交付给 Application Insights?

最佳答案

您需要实现自定义 ITelemetryProcessor,如下所述: https://learn.microsoft.com/en-gb/azure/azure-monitor/app/api-filtering-sampling#filtering

public class ControllerNotImplementedTelemetryProcessor : ITelemetryProcessor
{
    private ITelemetryProcessor Next { get; }

    public void Process(ITelemetry item)
    {
        var exception = item as ExceptionTelemetry;

        if (exception != null && exception.Exception.GetType() == typeof(HttpException) && exception.Exception.Message.Contains("was not found or does not implement IController"))
            return;

        Next.Process(item);
    }

    public ControllerNotImplementedTelemetryProcessor(ITelemetryProcessor next)
    {
        Next = next;
    }
}

在 Global.ascx 中注册自定义处理器:

TelemetryConfiguration.Active.TelemetryProcessorChainBuilder
    .Use(next => new ControllerNotImplementedTelemetryProcessor(next))
    .Build();

请注意,这显然是一个非常粗略的过滤器,根据错误消息的语言,英语可能是可以接受的,但也可能导致误报。

关于c# - 使用 Application Insights 抑制 "The controller for path '/' was not found or does not implement IController."服务器端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45838846/

相关文章:

c# - 在 C# 中从 base64 获取图像大小

javascript - 使用 ApplicationInsights 跟踪跨域 session

c# - PInvoking 时将参数声明为 [In] 有什么意义吗?

c# - 定时任务限制(或者任务持久化是如何实现的)?

c# - 如何为具有前导描述的应用程序元素实现 "Go To Definition"?

Azure Application Insights - 按时间戳的一部分进行汇总

用于在数据库池上设置警报的 Azure ARM 模板架构/文档

azure - 监视连续的 Azure Web 作业

c# - 如何在 Web api 2/app Insights 中生成相关 ID?

c# - Mono 命令行程序死于 "trace trap"消息,仅此而已