接收到的字节总是返回 0

Bytes Received always returning 0

我正在尝试使用 C# PerformanceCounter class 来 return 系统指标。

// Initialisation
// Threads (total threads for all processes)
PerformanceCounter performThreads = new System.Diagnostics.PerformanceCounter();
((ISupportInitialize)(performThreads)).BeginInit();
performThreads.CategoryName = "System";
performThreads.CounterName = "Threads";
((ISupportInitialize)(performThreads)).EndInit();

// Bytes received (cumulative total bytes received over all open socket connections)
private PerformanceCounter m_pcSys_BytesSent;
PerformanceCounter performBytesR = new System.Diagnostics.PerformanceCounter();
((ISupportInitialize)(performBytesR)).BeginInit();
performBytesR.CategoryName = ".NET CLR Networking";
performBytesR.CounterName = "Bytes Received";
performBytesR.InstanceName = "_global_";
((ISupportInitialize)(performBytesR)).EndInit();

// Later on ... periodically poll performance counters
long lThreads = performThreads.RawValue;    // Works!
long lBytesR = performBytesR.RawValue;      // Always returns 0 :o(

上面最后一行的工作原理是它不会抛出异常但总是 returns 0.

我已经尝试了 NextSampleNextValue,结果相同。如果我将 InstanceName 更改为进程名称,我会再次得到相同的结果。如果 InstanceName 设置为任何其他值,则在我调用 RawValue.

时会抛出异常 Instance 'XYZ' does not exist in the specified Category.

有什么想法吗?

根据Networking Performance Counters

Networking performance counters need to be enabled in the configuration file to be used.

If networking counters are enabled, this will create and update both per-AppDomain and global performance counters. If disabled, the application will not provide any networking performance counter data.