Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Performance Counter always return 0

Tags:

c#

asp.net

vb.net

I want to read the performance NextValue()'s of the "ASP.NET" performance counters category. However the counters of this category are always showing 0, whereas other counters work as expected.

The "ASP.NET" counters in perfmon.exe on the remote machine are working fine.

The "ASP.NET" counters in perfmon.exe 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 some kind of firewall issue?

like image 914
Mrks83 Avatar asked Oct 23 '13 13:10

Mrks83


2 Answers

The solution is to sleep 1 second between the calls to NextValue.

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's still returning the correct number, but it's very close (within 1 point) to what perfmon shows. I tried it with 2 seconds also and it appears to be 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(), as per the documentation!

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

If the calculated value of a counter depends on two counter reads, 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 the NextValue method is one second, to allow the counter to perform the next incremental read.

like image 178
Tony Hinkle Avatar answered Oct 21 '22 04:10

Tony Hinkle


If sleep calls between .NextValue() don´t work, you must put your user in the "Performance Monitor Users" group permission.

If it is a service, put user "Local Service" in the "Performance Monitor Users" group.

How to add user to Performance Monitor Users group

like image 27
Mario Matiusso Jr. Avatar answered Oct 21 '22 03:10

Mario Matiusso Jr.