Activity.Current 在 HttpRequestOut.Stop 上为空
Activity.Current is null on HttpRequestOut.Stop
我正在为控制台应用程序 (.NET Core 2.1) 使用 Application Insights。
我需要比 ApplicationInsights.DependencyCollector
收集更多关于依赖项的信息(requests/responses)。
所以我尝试了 this blog 中描述的方法。它适用于请求。
但它不适用于响应。因为在上面的代码中 Activity.Current
是 null
:
[DiagnosticName("System.Net.Http.HttpRequestOut.Stop")]
public virtual void OnHttpRequestOutStop(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpResponseMessage response, TaskStatus requestTaskStatus)
{
Console.WriteLine(Activity.Current);
}
虽然在 HttpRequestOut.Start
的类似代码中它具有正确的值:
[DiagnosticName("System.Net.Http.HttpRequestOut.Start")]
public virtual void OnHttpRequestOutStart(System.Net.Http.HttpRequestMessage request)
{
Console.WriteLine(Activity.Current);
}
为什么在 System.Net.Http.HttpRequestOut.Stop
事件中 Activity.Current
是 null
?
如何在 Start 和 Stop 事件中访问相同的 activity?
更新:
我找到了一些关于这个问题的信息 here。
This comment 真的很有帮助。
The most complicated scenario is when you want to access response and enrich telemetry based on it. you can still use diagnostic source Stop event, however, this becomes hacky because AppInsights listens to the same even and your listener needs to receive the Stop event before AppInsights.
所以我刚刚在遥测客户端之前初始化了我的 'enrichment' 观察器 class。 Activity.Current
在 OnHttpRequestOutStop
中不再是 null
。
我正在为控制台应用程序 (.NET Core 2.1) 使用 Application Insights。
我需要比 ApplicationInsights.DependencyCollector
收集更多关于依赖项的信息(requests/responses)。
所以我尝试了 this blog 中描述的方法。它适用于请求。
但它不适用于响应。因为在上面的代码中 Activity.Current
是 null
:
[DiagnosticName("System.Net.Http.HttpRequestOut.Stop")]
public virtual void OnHttpRequestOutStop(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpResponseMessage response, TaskStatus requestTaskStatus)
{
Console.WriteLine(Activity.Current);
}
虽然在 HttpRequestOut.Start
的类似代码中它具有正确的值:
[DiagnosticName("System.Net.Http.HttpRequestOut.Start")]
public virtual void OnHttpRequestOutStart(System.Net.Http.HttpRequestMessage request)
{
Console.WriteLine(Activity.Current);
}
为什么在 System.Net.Http.HttpRequestOut.Stop
事件中 Activity.Current
是 null
?
如何在 Start 和 Stop 事件中访问相同的 activity?
更新:
我找到了一些关于这个问题的信息 here。
This comment 真的很有帮助。
The most complicated scenario is when you want to access response and enrich telemetry based on it. you can still use diagnostic source Stop event, however, this becomes hacky because AppInsights listens to the same even and your listener needs to receive the Stop event before AppInsights.
所以我刚刚在遥测客户端之前初始化了我的 'enrichment' 观察器 class。 Activity.Current
在 OnHttpRequestOutStop
中不再是 null
。