Search code examples
c#asp.net-coreazure-application-insightsrestsharp

Dependency logging with application insights does not log responses


We have set up application insights with our ASP.NET Core 6 application which uses application insights for logging. We use a RestSharp based HTTP client for executing HTTP requests which works fine.

When trying to debug calls made through the RestSharp library, I am not seeing any response bodies being logged, only the statuscode, while the requests are being (kind of) logged:

App Insights logging

{
  "name": "AppDependencies",
  "time": "2023-02-02T06:05:04.6268266Z",
  "tags": {
    "ai.application.ver": "1.0.0.0",
    "ai.cloud.roleInstance": "MY_NICE_PC",
    "ai.user.id": "ltK4V",
    "ai.operation.id": "11bf52695a8d8ea19f1cb7573f2b195b",
    "ai.operation.parentId": "324234234234",
    "ai.operation.name": "POST to/somewhere [v]",
    "ai.location.ip": "::1",
    "ai.internal.sdkVersion": "rdddsc:2.21.0-429",
    "ai.internal.nodeName": "MY_NICE_PC"
  },
  "data": {
    "baseType": "RemoteDependencyData",
    "baseData": {
      "ver": 2,
      "name": "POST /none/of-your/business",
      "id": "bfa554335eefae0b",
      "data": "https://none.of.your.business/my-nice-api-0/my=1&nice&=2&querystring=3",
      "duration": "00:00:04.8666247",
      "resultCode": "422",
      "success": false,
      "type": "Http",
      "target": "none.of.your.business",
      "properties": {
        "DeveloperMode": "true",
        "AspNetCoreEnvironment": "localdev",
        "_MS.ProcessedByMetricExtractors": "(Name:'Dependencies', Ver:'1.1')"
      }
    }
  }
}

We are using ASP.NET Core 6.0 with the Microsoft.ApplicationInsights.AspNetCore version 2.2.21 and the following DI setup:

services.AddApplicationInsightsTelemetry(configure =>
            {
                configure.ConnectionString = "MySecretConnectionString";
                configure.EnableAdaptiveSampling = false;
            });   

--- Edit: I also made an implementation of the ITelemetryInitializer to capture all telemetry for all instances of DependencyTelemetry:

public class DependencyTelemetryInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            // SKIP for now
            if (telemetry is DependencyTelemetry dependencyTelemetry)
            {

            }
            
        }
    }

This showed me that for every dependency call only the request is being captured, but the response is not.


Solution

  • Finally I managed to fix this, the issue with not being able to log large response bodies had to to with treading, I fixed by not waiting for the task to complete, this works for me:

     private static async Task TryUpdateTelemetryWithResponseBody(DependencyTelemetry dependencyTelemetry, HttpResponseMessage response)
            {
                await Task.Run(async () =>
                {
                    await response.Content.LoadIntoBufferAsync();
                    var stream = await response.Content.ReadAsStreamAsync();
    
                    using var reader = new StreamReader(
                      stream,
                      Encoding.UTF8,
                      detectEncodingFromByteOrderMarks: false,
                      bufferSize: 8192, leaveOpen: true);
    
                    var responseBody = await reader.ReadToEndAsync();
    
                    stream.Seek(0, SeekOrigin.Begin);
    
                    dependencyTelemetry.Properties.Add("responseBody", responseBody);
                    dependencyTelemetry.Success = true;
                });
            }