我有一个WPF应用程序,我想使用Application Insights进行监控 . 我想将每个用户操作分组为一个请求,包含嵌入式自定义事件和依赖项调用 . 从我在documentation操作上下文中读到的内容应该是正确的方法 . 我写了以下代码:
using (var operation = telemetryClient.StartOperation<RequestTelemetry>(
$"test operation {Guid.NewGuid()}"))
{
telemetryClient.TrackEvent("my test event");
telemetryClient.StopOperation(operation);
}
不幸的是,当我检查Application Insights门户时,“我的测试事件”并未显示为请求事件的相关事件 . 我进行了一些实验,发现如果我手动设置操作属性,事件就会变得相关:
using (var operation = telemetryClient.StartOperation<RequestTelemetry>(
$"test operation {Guid.NewGuid()}"))
{
var ev = new EventTelemetry("test event");
ev.Context.Operation.Id = operation.Telemetry.Context.Operation.Id;
ev.Context.Operation.Name = operation.Telemetry.Context.Operation.Name;
telemetryClient.TrackEvent(ev);
telemetryClient.StopOperation(operation);
}
但我觉得它不应该是这样的 . 所以我的问题是:有人能指出我在第一个片段中做错了什么吗?是否在 TelemetryClient
上有一些神奇的设置,使其表现如文档中所述?
1 回答
确保在相同上下文中的所有传出遥测具有相同操作ID,名称等的相对简单的方式是使用具有CallContext的TelemetryInitializer .
This is a really good blog post显示了如何做到这一点 .