ASP.NET performance counter always returns 0

I want to read the performance of NextValue() in the ASP.NET performance counter category. However, counters in this category always show 0, while other counters work as expected.

The ASP.NET counters in the perfmon.exe file on the remote machine work fine.

The ASP.NET counters in the perfmon.exe file on my local machine targeting the remote machine also show 0.

 var pc = new PerformanceCounter("ASP.NET", "Requests Current", "", "myRemoteMachine"); pc.NextValue(); // returns always 0 pc.NextValue(); // returns always 0 

Any ideas? Permission or any problem with the firewall?

+6
source share
1 answer

The solution is to sleep 1 second between NextValue calls.

In VB:

 Dim cpu As New PerformanceCounter("Processor", "% Processor Time", "_Total", "servername") cpu.NextValue() System.Threading.Thread.Sleep(1000) MyValue = cpu.NextValue() 

It's hard to know if it will return the correct number, but it is very close (within 1 point) to what perfmon shows. I tried this with 2 seconds as well, and it seems a bit closer to what perfmon shows.

From http://blogs.msdn.com/b/dotnetinterop/archive/2007/02/02/system-diagnostics-performancecounter-and-processor-time-on-multi-core-or-multi-cpu.aspx :

Keep in mind that you need to delay "about 1 second" between calls to NextValue (), according to the documentation!

... and links to https://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.nextvalue.aspx , which states:

If the calculated counter value depends on two counters, the first read operation returns 0.0. Resetting the performance counter properties to specify a different counter is equivalent to creating a new performance counter, and the first read operation using the new properties returns 0.0. The recommended delay time between calls to NextValue is one second to allow the counter to perform the next incremental read.

+1
source

Source: https://habr.com/ru/post/956562/


All Articles