Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Azure 如何在Application Insights中扩展传出http请求的依赖项跟踪_Azure_.net Core_Azure Application Insights - Fatal编程技术网

Azure 如何在Application Insights中扩展传出http请求的依赖项跟踪

Azure 如何在Application Insights中扩展传出http请求的依赖项跟踪,azure,.net-core,azure-application-insights,Azure,.net Core,Azure Application Insights,我有一个.NET核心API,它执行到其他API的HTTP连接。我能够在ApplicationInsights中的DependencyEventTypes下可视化传出HTTP请求,但它只有基本信息。我正在研究如何添加有关传出HTTP调用的更多信息(例如HTTP头) 我已经研究过了,但没有找到任何具体的方法。我想您正在寻找的是,它可以为依赖项遥测添加自定义属性 对于.net核心web项目,您可以参考以下内容 我编写了一个演示,如下所示: 1.创建自定义ITelemetryInitializer类以收

我有一个.NET核心API,它执行到其他API的HTTP连接。我能够在ApplicationInsights中的DependencyEventTypes下可视化传出HTTP请求,但它只有基本信息。我正在研究如何添加有关传出HTTP调用的更多信息(例如HTTP头)


我已经研究过了,但没有找到任何具体的方法。

我想您正在寻找的是,它可以为依赖项遥测添加自定义属性

对于.net核心web项目,您可以参考以下内容

我编写了一个演示,如下所示:

1.创建自定义ITelemetryInitializer类以收集任何依赖项数据:

    public class MyTelemetryInitializer: ITelemetryInitializer
    {
        IHttpContextAccessor httpContextAccessor;

        public MyTelemetryInitializer(IHttpContextAccessor httpContextAccessor)
        {
            this.httpContextAccessor = httpContextAccessor;
        }

        public void Initialize(ITelemetry telemetry)
        {
            //only add custom property to dependency type, otherwise just return.
            var dependencyTelemetry = telemetry as DependencyTelemetry;
            if (dependencyTelemetry == null) return;

            if (!dependencyTelemetry.Context.Properties.ContainsKey("custom_dependency_headers_1"))
            {
                //the comment out code use to check the fields in Headers if you don't know
                //var s = httpContextAccessor.HttpContext.Request.Headers;
                //foreach (var s2 in s)
                //{
                //   var a1 = s2.Key;
                //    var a2 = s2.Value;
                //}

                dependencyTelemetry.Context.Properties["custom_dependency_headers_1"] = httpContextAccessor.HttpContext.Request.Headers["Connection"].ToString();
            }
        }

    }
public void ConfigureServices(IServiceCollection services)
{
//other code

//add this line of code here
services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();
}
2。然后在Startup.cs->ConfigureServices方法中:

    public class MyTelemetryInitializer: ITelemetryInitializer
    {
        IHttpContextAccessor httpContextAccessor;

        public MyTelemetryInitializer(IHttpContextAccessor httpContextAccessor)
        {
            this.httpContextAccessor = httpContextAccessor;
        }

        public void Initialize(ITelemetry telemetry)
        {
            //only add custom property to dependency type, otherwise just return.
            var dependencyTelemetry = telemetry as DependencyTelemetry;
            if (dependencyTelemetry == null) return;

            if (!dependencyTelemetry.Context.Properties.ContainsKey("custom_dependency_headers_1"))
            {
                //the comment out code use to check the fields in Headers if you don't know
                //var s = httpContextAccessor.HttpContext.Request.Headers;
                //foreach (var s2 in s)
                //{
                //   var a1 = s2.Key;
                //    var a2 = s2.Value;
                //}

                dependencyTelemetry.Context.Properties["custom_dependency_headers_1"] = httpContextAccessor.HttpContext.Request.Headers["Connection"].ToString();
            }
        }

    }
public void ConfigureServices(IServiceCollection services)
{
//other code

//add this line of code here
services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();
}
public void配置服务(IServiceCollection服务)
{
//其他代码
//在这里添加这行代码
services.AddSingleton();
}
3.测试结果,检查自定义属性是否添加到azure门户->自定义属性:

    public class MyTelemetryInitializer: ITelemetryInitializer
    {
        IHttpContextAccessor httpContextAccessor;

        public MyTelemetryInitializer(IHttpContextAccessor httpContextAccessor)
        {
            this.httpContextAccessor = httpContextAccessor;
        }

        public void Initialize(ITelemetry telemetry)
        {
            //only add custom property to dependency type, otherwise just return.
            var dependencyTelemetry = telemetry as DependencyTelemetry;
            if (dependencyTelemetry == null) return;

            if (!dependencyTelemetry.Context.Properties.ContainsKey("custom_dependency_headers_1"))
            {
                //the comment out code use to check the fields in Headers if you don't know
                //var s = httpContextAccessor.HttpContext.Request.Headers;
                //foreach (var s2 in s)
                //{
                //   var a1 = s2.Key;
                //    var a2 = s2.Value;
                //}

                dependencyTelemetry.Context.Properties["custom_dependency_headers_1"] = httpContextAccessor.HttpContext.Request.Headers["Connection"].ToString();
            }
        }

    }
public void ConfigureServices(IServiceCollection services)
{
//other code

//add this line of code here
services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();
}

谢谢伊万!我已经尝试过了,但它似乎只适用于对我的API的传入请求。我无法处理从我的API到第三方API的传出请求。我会继续尝试。只要问“我的api发出的请求”是不是依赖遥测?是的,这是依赖遥测。我已经根据你的建议编辑了这篇文章。你能给我一些关于将发出的请求进行依赖遥测的说明吗?我可以试着重现这个问题。你找到解决这个问题的方法了吗?我也在寻找一种方法来做到这一点。@WesselKranenborg:不太可能。最后我不得不把它们分开记录