C# 禁用特定方法的Application Insights依赖项跟踪

C# 禁用特定方法的Application Insights依赖项跟踪,c#,azure-application-insights,C#,Azure Application Insights,是否可以禁用特定方法/函数的Application Insights依赖项跟踪 我的具体问题是,我有一个自定义ITelemetryInitializer,在其中,我调用一个静态函数,如果该值尚未缓存在内存中,则该函数可能具有外部依赖关系。在这个函数中,它创建了一个新的ITelemetry并调用了我的ITelemetryInitializer,它生成了一个新的ITelemetry,等等,结果是StackOverflowException 基本上(这不是我的实际代码): 我正在修改我的初始值设定项,

是否可以禁用特定方法/函数的Application Insights依赖项跟踪

我的具体问题是,我有一个自定义ITelemetryInitializer,在其中,我调用一个静态函数,如果该值尚未缓存在内存中,则该函数可能具有外部依赖关系。在这个函数中,它创建了一个新的ITelemetry并调用了我的ITelemetryInitializer,它生成了一个新的ITelemetry,等等,结果是StackOverflowException

基本上(这不是我的实际代码):


我正在修改我的初始值设定项,使其不使用外部依赖项,但我认为这个问题仍然有效-如果有我不想跟踪的特定内容,我可以禁用该内容的遥测功能吗(在我的示例中,我想关闭
GetData
方法的跟踪功能)?

可以使用ITelemetryProcessor()禁用依赖项跟踪

但这对您的情况没有帮助,因为无论如何都会首先调用ITelemetryInitializer。正确的解决方案是不从ITelemetryInitializer执行依赖项调用

public void Initialize(ITelemetry telemetry)
{
    var cached = GetCachedValue();
    if(cached)
        return cached;
    else
    {
        var value = GetData();  // New Telemetry gets created here - since it's not cached yet, the new telemetry gets initialized and goes right back here.
        SetCachedValue(value);
    }
}