从 .NET Framework Windows 性能计数器迁移到 .NET 指标

仅在 Windows 上运行的 .NET Framework 应用程序可以使用 Windows 性能计数器来监视应用程序运行状况和性能。 但是,在 .NET Core 和更高版本中,该平台通过 EventCountersSystem.Diagnostics.Metrics API 提供跨平台替代项。

本文提供有关从 Windows 性能计数器迁移到新式 .NET 版本中提供的较新的指标系统的指南。

.NET 指标的简短历史记录

可以使用不同的机制监视 .NET 应用程序,具体取决于 .NET 版本和平台:

较新的指标系统具有多种优势:

  • 跨平台操作:支持 Windows、Linux 和 macOS
  • 容器友好:适用于容器化环境
  • 新式工具:与 OpenTelemetry 和可观测性平台集成
  • 支持 xcopy 安装:无需其他安装步骤或权限

有关详细信息,请参阅 指标 API 比较

在新式 .NET 应用程序中收集指标

若要收集和分析指标,请参阅 System.Diagnostics.MetricsEventCounters 指南。

将常见的 Windows 性能计数器映射到新式指标

如果 .NET Framework 应用程序的监视系统使用运行时提供的 Windows 性能计数器,则需要改为选择备用 EventCounters基于 System.Diagnostics.Metrics 的指标。 下表显示了许多常见计数器的替代方法。 并非所有 .NET Framework 计数器都已移植到新的替代项。 在某些情况下,不经常使用的计数器已停止使用,在其他情况下,平台中的实现更改使某些计数器无关。

内存计数器

Windows 性能计数器 EventCounter 等效项 指标 API 等效
.NET CLR Memory\# Bytes in all Heaps System.Runtime\GC Heap Sizegc-heap-size System.Runtime\dotnet.gc.last_collection.heap.size
.NET CLR Memory\# Gen 0 Collections System.Runtime\Gen 0 GC Countgen-0-gc-count System.Runtime\dotnet.gc.collections 具有属性 gc.heap.generation=gen0
.NET CLR Memory\# Gen 1 Collections System.Runtime\Gen 1 GC Countgen-1-gc-count System.Runtime\dotnet.gc.collections 具有属性 gc.heap.generation=gen1
.NET CLR Memory\# Gen 2 Collections System.Runtime\Gen 2 GC Countgen-2-gc-count System.Runtime\dotnet.gc.collections 具有属性 gc.heap.generation=gen2
.NET CLR Memory\% Time in GC System.Runtime\% Time in GC since last GCtime-in-gc System.Runtime\dotnet.gc.pause.time (以总时间的百分比进行计算)
.NET CLR Memory\# Total committed Bytes 没有 System.Runtime\dotnet.gc.last_collection.memory.committed_size
.NET CLR Memory\Large Object Heap Size System.Runtime\LOH Sizeloh-size System.Runtime\dotnet.gc.last_collection.heap.size 具有属性 gc.heap.generation=loh
.NET CLR Memory\Allocated Bytes/sec System.Runtime\Allocation Ratealloc-rate System.Runtime\dotnet.gc.heap.total_allocated 计算速率

注释

dotnet.gc.pause.time 允许改进的计算,避免旧 % Time in GC 指标中出现一些不良行为。 % Time in GC 计算出 100 * pause_time_in_most_recent_GC / time_between_most_recent_two_GCs. 在某些情况下,两个 GC 会非常接近地发生,这会根据整个时间间隔中的一个微小且不具代表性的部分生成一个高值。 gc.heap.pause.time 累积 GC 到目前为止在进程中暂停应用程序线程的总时间,这样就可以在任何测量的时间间隔内计算 GC 暂停时间。 这是对 GC 开销的更真实的度量,但计算中的更改意味着即使基础 GC 行为不变,指标也可能不匹配。

JIT 和加载计数器

Windows 性能计数器 EventCounter 等效项 指标 API 等效
.NET CLR Jit\# of Methods Jitted System.Runtime\Methods Jitted Countmethods-jitted-count System.Runtime\dotnet.jit.compiled_methods
.NET CLR Jit\IL Bytes Jitted System.Runtime\IL Bytes Jittedil-bytes-jitted System.Runtime\dotnet.jit.compiled_il.size
.NET CLR Loading\Current Assemblies System.Runtime\Number of Assemblies Loadedassembly-count System.Runtime\dotnet.assembly.count
.NET CLR Jit\Total # of IL Bytes Jitted System.Runtime\IL Bytes Jittedil-bytes-jitted System.Runtime\dotnet.jit.compiled_il.size

线程计数器

Windows 性能计数器 EventCounter 等效项 指标 API 等效
.NET CLR LocksAndThreads\Current Queue Length System.Runtime\ThreadPool Queue Lengththreadpool-queue-length System.Runtime\dotnet.thread_pool.queue.length
.NET CLR LocksAndThreads\Contention Rate / sec System.Runtime\Monitor Lock Contention Countmonitor-lock-contention-count System.Runtime\dotnet.monitor.lock_contentions 计算速率

异常计数器

Windows 性能计数器 EventCounter 等效项 指标 API 等效
.NET CLR Exceptions\# of Exceps Thrown / sec System.Runtime\Exception Countexception-count System.Runtime\dotnet.exceptions 计算速率
.NET CLR Exceptions\# of Exceps Thrown 没有 System.Runtime\dotnet.exceptions

套接字网络计数器

Windows 性能计数器 EventCounter 等效项 指标 API 等效
.NET CLR Networking\Bytes Received System.Net.Sockets\Bytes Receivedbytes-received 没有
.NET CLR Networking\Bytes Sent System.Net.Sockets\Bytes Sentbytes-sent 没有
.NET CLR Networking\Connections Established System.Net.Sockets\Outgoing Connections Establishedoutgoing-connections-established 没有
.NET CLR Networking\Datagrams Received System.Net.Sockets\Datagrams Receiveddatagrams-received 没有
.NET CLR Networking\Datagrams Sent System.Net.Sockets\Datagrams Sentdatagrams-sent 没有

DNS 网络计数器

Windows 性能计数器 EventCounter 等效项 指标 API 等效
.NET CLR Networking\DNS Lookups System.Net.NameResolution\DNS Lookups Requesteddns-lookups-requested System.Net.NameResolution\dns.lookup.duration中求和直方图桶
.NET CLR Networking\DNS Resolution Time System.Net.NameResolution\Average DNS Lookup Durationdns-lookups-duration System.Net.NameResolution\dns.lookup.duration

HttpWebRequest 计数器

HttpWebRequest 已被取代 HttpClient。 若要了解内置指标,请参阅 HttpClient EventCountersSystem.Diagnostics.Metrics

ASP.NET 计数器

ASP.NET 在 .NET Framework 和 .NET Core 之间发生了巨大变化。 许多计数器已过时或测量方式与过去不同。 若要了解内置指标,请参阅 ASP.NET EventCountersSystem.Diagnostics.Metrics

后续步骤