c# - 具有应用程序见解的依赖关系日志记录不会记录响应

标签 c# asp.net-core azure-application-insights restsharp

我们已经使用 ASP.NET Core 6 应用程序设置了应用程序洞察,该应用程序使用应用程序洞察进行日志记录。我们使用基于 RestSharp 的 HTTP 客户端来执行 HTTP 请求,效果很好。

当尝试调试通过 RestSharp 库进行的调用时,我没有看到任何响应主体被记录,只有状态代码,而请求正在被(某种)记录:

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

我们使用 ASP.NET Core 6.0 和 Microsoft.ApplicationInsights.AspNetCore 版本 2.2.21 以及以下 DI 设置:

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

--- 编辑: 我还实现了 ITelemetryInitializer 来捕获 DependencyTelemetry 所有实例的所有遥测数据:

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

            }
            
        }
    }

这表明,对于每个依赖项调用,只有请求被捕获,但响应没有

最佳答案

按照设计,依赖项调用基本上会记录传出调用,因为这是应用程序见解可以访问的内容。它记录持续时间以及响应代码(在您的示例中为 422)。我也不确定您希望从响应中看到什么样的信息。

如果您想访问响应,您可以在 TelemetryInitializer 中执行此操作。 :

public class CustomInitializer : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        if (telemetry is DependencyTelemetry dt
                && dt.Type.Equals("http", System.StringComparison.InvariantCultureIgnoreCase)
                && dt.TryGetHttpResponseOperationDetail(out var response))
        {
            ...
        }
    }
}

然后您可以访问 header 等。从技术上讲,您可以访问响应内容,但由于它默认是流,因此只能读取。因此,在不破坏应用程序的情况下,无法在此处阅读完整的响应内容。

此外,即使可以,您也应该小心记录请求和响应播放负载。大小可能很大,并且 Application Insights 不适用于此类日志数据。您也可以点击the limits to the lenght of the custom properties并且记录的有效负载将被切断。

关于c# - 具有应用程序见解的依赖关系日志记录不会记录响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75319336/

相关文章:

c# - 如何确保对象在没有构造函数的情况下正确初始化?

c# - 如何在 .NET Core 中使用 HttpWebResponse 和 WebResponse?

c# - 带有 Blazor : Cannot figure out cookie authentification 的 ASP.NET Core

azure - 应用程序洞察跟踪指标不起作用?

azure - 在 Azure Function App 中配置 Application Insights 的云角色名称

Azure Application Insights 云角色名称到 API 管理

c# - WPF 窗口 - 何时加载数据以便立即显示表单

时间:: accessing parent class's events not possible?

c# - 我如何只允许设置某些消息的用户 [NODE.JS]

razor - AddRazorPagesOptions Conventions.AddPageRoute 中的野猫