如何解决使用 SDK 时 AppInsights 跟踪请求操作名称不同
我一直在我们的网站上使用 Azure AppInsights 插件(您只需在应用服务配置上打开它,不需要代码)并且正在获取我用于过滤和报告的请求的完整操作名称(我过滤路线 id,每个 id 都是不同的来源,我们使用它来跟踪流量),如下面的屏幕截图所示:
操作名称:GET /join/social
Screenshot of request details with full operation name
最近我不得不向请求添加自定义全局属性,所以我改为使用 SDK,这样我就可以手动添加我需要的任何内容。现在请求操作名称如下面的屏幕截图所示:
操作名称:GET CustomerCoCreation/Join [id]
Screenshot of request details with a different operation name
它现在更像是一个路线定义,不再显示 id,这是我们报告所需要的,因此我已经创建的每个报告和自定义仪表板都变得毫无用处。
这是我的 TelemetryInitializer,非常标准,只是添加了我们想要的自定义属性(URL 引用):
public class RefererTelemetryInitializer : ITelemetryInitializer
{
private const string HeaderNameDefault = "Referer";
private readonly IHttpContextAccessor _httpContextAccessor;
public RefererTelemetryInitializer(IHttpContextAccessor httpContextAccessor)
{
if (httpContextAccessor == null) throw new System.ArgumentNullException(nameof(httpContextAccessor));
_httpContextAccessor = httpContextAccessor;
}
public void Initialize(ITelemetry telemetry)
{
var context = _httpContextAccessor.HttpContext;
if (context == null)
return;
if (context.Request.Headers.TryGetValue(HeaderNameDefault,out var value))
{
telemetry.Context.GlobalProperties["Referer"] = value.ToString();
}
}
}
这就是我在 dotnet Core 应用程序的 ConfigureServices 中初始化它的方式:
services.AddApplicationInsightsTelemetry();
services.AddSingleton<ITelemetryInitializer,RefererTelemetryInitializer>();
有什么方法可以设置或配置我的初始化程序以模仿它以前的样子吗?
提前致谢!
解决方法
在我看来,您可以添加如下自定义属性,它可以实现您的目标吗?
public void Initialize(ITelemetry telemetry)
{
telemetry.Context.GlobalProperties["RequestId"] = "customId";
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。