2
votes

I have a .NET core API that performs HTTP connections to other API. I am able to visualize the outgoing HTTP request in Application Insights, under Dependency Event Types, but it has only basic information. I'm looking on how to add more information about the outgoing HTTP call (like the HTTP headers for instance).

I've looked into https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics#trackdependency but I didn't find any concrete way of doing this.

1
Did you find a solution to fix this? I'm also searching for a way to do this.Wessel Kranenborg
@WesselKranenborg: Not really. Eventually I had to log them separatelykord

1 Answers

1
votes

I think what you're looking for is ITelemetryInitializer, which can add custom property for dependency telemetry.

And for .net core web project, you can refer to this link.

I write a demo as below:

1.Create a custom ITelemetryInitializer class to collect any dependency data:

    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();
            }
        }

    }

2.Then in the Startup.cs -> ConfigureServices method:

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

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

3.Test result, check if the custom property is added to azure portal -> Custom Properties:

enter image description here