You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Application Insights记录RestSharp依赖时无法捕获响应体

ASP.NET Core 6中Application Insights无法捕获RestSharp请求的响应体问题

我们的ASP.NET Core 6应用已集成Application Insights用于日志记录,同时使用基于RestSharp的HTTP客户端执行HTTP请求,客户端运行正常。但调试RestSharp发起的调用时,发现仅能记录状态码,无法捕获响应体,请求信息已被部分记录:

App Insights日志记录

{
  "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')"
      }
    }
  }
}

我们使用ASP.NET Core 6.0,搭配版本为2.2.21的Microsoft.ApplicationInsights.AspNetCore,依赖注入配置如下:

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

补充说明:
我还实现了ITelemetryInitializer来捕获所有DependencyTelemetry实例的遥测数据:

public class DependencyTelemetryInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            // 暂时跳过
            if (telemetry is DependencyTelemetry dependencyTelemetry)
            {

            }
            
        }
    }

该实现显示,每个依赖调用仅能捕获请求信息,无法捕获响应信息。

内容的提问来源于stack exchange,提问作者Wilko van der Veen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 08:45:20