我想通过Azure-ApplicationInsights报告每种路由的一些属性,如下图所示:
问题在于,即使我在同一条路线或不同路线上进行第二次呼叫,这些值也不会改变。仅在第三个调用时,该值才会更改,但是即使在当前调用时,该值也不会更改。 示例:
后面的代码如下:
[Route("api/[controller]")]
[ApiController]
public class TestObjectController
{
private TelemetryClient telemetryClient;
public TestObjectController(TelemetryClient telemetryClient)
{
this.telemetryClient = telemetryClient;
}
// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
telemetryClient.Context.Properties.Clear();
telemetryClient.Context.GlobalProperties["TestObjectController"] = "nok";
telemetryClient.Context.Properties["proprietate custom"] = $"valoare custom {id}";
return "value";
}
}
任何想法如何使其准确?
答案 0 :(得分:2)
telemetryClient.Context.Properties
仅在Telemetry Client实例上设置上下文。这意味着在新的遥测项生效之前,可能会使用先前的上下文创建多个遥测项(如果在上下文更新之前创建了遥测项)。
情况似乎是这样-请求遥测项是在方法调用时创建的,并且仅在方法内部稍后,客户端才会更新为具有新的上下文。
我建议使用TelemetryInitializer
代替to update the context on the level of the Telemetry Item,而不要使用遥测客户端:
using System;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
namespace MvcWebRole.Telemetry
{
/*
* Custom TelemetryInitializer that overrides the default SDK
* behavior of treating response codes >= 400 as failed requests
*
*/
public class MyTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
var requestTelemetry = telemetry as RequestTelemetry;
// Is this a TrackRequest() ?
if (requestTelemetry == null) return;
int code;
bool parsed = Int32.TryParse(requestTelemetry.ResponseCode, out code);
if (!parsed) return;
if (code >= 400 && code < 500)
{
// If we set the Success property, the SDK won't change it:
requestTelemetry.Success = true;
// Allow us to filter these requests in the portal:
requestTelemetry.Context.Properties["Overridden400s"] = "true";
}
// else leave the SDK to set the Success property
}
}
}
然后,通过ApplicationInsights.config或在代码中将此初始化器添加到AI:
<ApplicationInsights>
<TelemetryInitializers>
<!-- Fully qualified type name, assembly name: -->
<Add Type="MvcWebRole.Telemetry.MyTelemetryInitializer, MvcWebRole"/>
...
</TelemetryInitializers>
</ApplicationInsights>
protected void Application_Start()
{
// ...
TelemetryConfiguration.Active.TelemetryInitializers
.Add(new MyTelemetryInitializer());
}
答案 1 :(得分:0)
@ [Dmitry Matveev]谢谢,但是您的建议部分有效,但是我更感兴趣的是从请求内容中添加某些信息(例如,租户名称)。为此,我最终在HttpContext.Items中添加了信息,然后通过“ MyTelemetryInitializer”访问它们,如以下链接中所述:https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Custom-Configuration#track-custom-traceeventmetric
将信息添加到路线的代码:
If ActiveCell >= 1 And ActiveCell.Offset(0, 1) <> "" Then
“ MyTelemetryInitializer”初始化方法的一部分:
If (ActiveCell = 1) Or (ActiveCell = 2) Then
MailBody = "ALERTA PRAZO ETAPA 4!!" & vbNewLine & vbNewLine & "Nº GQE " & Cells(ActiveCell.Row, 2).Value & " - " & Cells(ActiveCell.Row, 3).Value
ElseIf (ActiveCell >= 3) Then
MailBody = "ULTRAPASSADO PRAZO ETAPA 4!!" & vbNewLine & vbNewLine & "Nº GQE " & Cells(ActiveCell.Row, 2).Value & " - " & Cells(ActiveCell.Row, 3).Value
End If