【发布时间】:2022-01-03 13:25:40
【问题描述】:
我正在使用下面的代码来启用System.Net.Http.HttpClients 为每个传出的 HTTP 请求创建一个活动。这适用于面向 .NET Core 3.1 的 ASP.NET Core 应用程序,但不适用于面向 .NET Framework 4.7.2 的“经典”ASP.NET 应用程序。
// Using the Subscribe(IObservable<T>, Action<T>) extension method from the System.Reactive package
DiagnosticListener.AllListeners.Subscribe(listener =>
{
if (listener.Name == "HttpHandlerDiagnosticListener")
{
listener.Subscribe(diagnostic =>
{
if (diagnostic.Key == "System.Net.Http.HttpRequestOut.Start")
{
// ...
}
else if (diagnostic.Key == "System.Net.Http.HttpRequestOut.Stop")
{
// ...
}
}
}
}
我能想到的主要区别是 ASP.NET Core 应用程序使用 .NET Core 内置的 DiagnosticListener 类型,而“经典”ASP.NET 应用程序需要对 System 的包引用.Diagnostics.DiagnosticSource 使用该类型。
我尝试使用不同版本的 System.Diagnostics.DiagnosticSource 包(4.6.0、4.7.0、4.7.1、5.0.0 和 5.0.1),在每次升级/降级包后重建我的项目,但无济于事。
我找到了GitHub issue about a similar problem in Azure Functions (in-process) applications。他们提到降级到 System.Diagnostics.DiagnosticSource 4.7.0 作为一种解决方法,但在我的测试中不起作用。
【问题讨论】: