我正在尝试使用C#PerformanceCounter类来返回系统度量标准。收到的字节总是返回0
// 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(
以上在这个意义上的作品,它并不会抛出异常,但总是返回0
我曾经尝试都NextSample
和NextValue
具有相同结果的最后一行。如果我将InstanceName
更改为进程名称,我再次获得相同的结果。如果InstanceName
设置为其他任何内容,则在拨打RawValue
时引发异常Instance 'XYZ' does not exist in the specified Category.
。
任何想法?
使用嗅探器如fiddler或wireshark来验证数据是通过网络发送/接收的。 – jdweng
@AlainD:如果您正在收集系统级指标,请查看[Statsify](https://bitbucket.org/aeroclub-it/statsify)是否符合您的需求。 –
@jdweng:由于我使用Windows套接字连接到各种服务器,因此肯定会发送/接收数据。我可以计算手动发送和接收的字节数(并且它们不为零)。问题的结果是,按照Anton的回答,必须特别启用.NET网络性能计数器。 – AlainD