gpt4 book ai didi

azure - 如何向 HTTPS 调用添加自定义属性,以便我可以在 Azure Application Insights 中查看它们?

转载 作者:行者123 更新时间:2023-12-03 06:51:23 32 4
gpt4 key购买 nike

我有一个 C# 客户端应用程序,它调用 Azure 中的 Web 应用程序(也是用 C#、ASP .NET Core 编写的)。在 Azure Log Analytics 中捕获流量,以便我可以在 Azure Application Insights 中查看它。

当从我的客户端应用程序调用 Web 应用程序时,我想添加一些有关客户端应用程序版本的信息。我希望可以在 Application Insights 中轻松查看此信息。

我注意到 Application Insights 中的请求表有一个名为customDimensions的列。也许我可以把东西放在那里,但我不知道怎么做。

我的想法是将此信息添加为 header ,并以某种方式配置应用程序以将此信息从 header 复制到 customDimensions 中。这是一个好方法吗?如果是这样,我该如何完成后半部分(配置应用程序以将此信息从标题复制到 customDimensions 中)?

最佳答案

您可以为此编写一个遥测初始化程序,如 the docs 中所述。 :

Use telemetry initializers to enrich telemetry with additional information or to override telemetry properties set by the standard telemetry modules.

有一个基类 ( TelemetryInitializerBase ),您可以使用它提供对 http 上下文的访问以获取 header 的详细信息:

public class CustomInitializer : TelemetryInitializerBase
{
public CustomInitializer(IHttpContextAccessor contextAccessor) : base(contextAccessor)
{

}

protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
{
var appVersion = platformContext.Request.Headers["x-app-version"].ToString();
if(!string.IsNullOrWhiteSpace(appVersion))
requestTelemetry.Properties["appVersion"] = appVersion;
}
}

将此初始化程序添加到 Azure 中运行的 Web 应用程序中。然后,在请求遥测的 customDimensions 字段中,您可以找到 appVersion 的值。

另一个选项是在 Controller 操作中设置自定义属性,如下所示:

var appVersion = Request.Headers["x-app-version"].ToString();
if (!string.IsNullOrWhiteSpace(appVersion))
{
var requestTelemetry = HttpContext.Features.Get<RequestTelemetry>();
requestTelemetry.Properties.Add("appVersion", appVersion);
}

关于azure - 如何向 HTTPS 调用添加自定义属性,以便我可以在 Azure Application Insights 中查看它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73619797/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com