自定义遥测处理器未加载

Custom Telemetry Processor not loading

我编写了一个小型遥测处理器来过滤掉一些 HTTP 404,这对我的应用程序来说不是问题。我一直在关注 Microsoft's doc 这个过程。

我创建了一个新的 class 实现 ITelemetryProcessor 并设置了构造函数和处理函数。

namespace My_App
{
    class WebJobQueuesDependencyFilter : ITelemetryProcessor
    {
        private ITelemetryProcessor Next { get; set; }

        // Link processors to each other in a chain.
        public WebJobQueuesDependencyFilter(ITelemetryProcessor next)
        {
            this.Next = next;
        }

        public void Process(ITelemetry item)
        {
            var request = item as RequestTelemetry;

            if (request != null &&
            request.ResponseCode.Equals("404", StringComparison.OrdinalIgnoreCase))
            {
                // To filter out an item, just terminate the chain:
                return;
            }
            // Send everything else:
            this.Next.Process(item);
        }
    }
}

我还在 <TelemetryProcessors>

下的 ApplicationInsights.config 添加了新行
<Add Type="My_App.WebJobQueuesDependencyFilter, My_App" />

启动我的程序后,我在 Visual Studio 中看到以下消息记录在 AI 中。

AI: ApplicationInsights configuration file loading failed. Type 'My_App.WebJobQueuesDependencyFilter, My_App' was not found. Type loading was skipped. Monitoring will continue.

我怀疑问题出在配置中的行。 Microsoft 的文档似乎没有详细说明该行应包含的内容。我基于 MS 示例的猜测是过滤器 class 的命名空间路径和我的应用程序的命名空间。

您可能忘记标记 class public,所以找不到该类型。将遥测处理器更新为 public:

public class WebJobQueuesDependencyFilter : ITelemetryProcessor

此外,指定类型的正确方法包括遥测处理器的完全限定名称后跟程序集名称。因此,在您的示例中,程序集名称必须是 My_App

<!-- Fully qualified type name, assembly name: -->
<Add Type="My_App.WebJobQueuesDependencyFilter, My_App" />

希望对您有所帮助!

原来我的问题有两部分。

我声明遥测处理器的方式不正确, 已更正。

我发现的第二个问题是自定义遥测处理器的 Process 方法永远不会被调用,除非您在配置中设置了 InstrumentationKey。在本地开发时留空是行不通的。

有一个issue on the Application Insights Github for this which has been resolved