Performance counter shows different values ​​when ReadOnly = false

I tried to find out why some performance counters are not updated on our production server when I got into this strange problem. It seems that the counter returns different values RawValuewhen it is not read-only. When it is read-only, it is always zero, when it is not read-only, it shows different values.

Here is my PowerShell session showing this:

PS C:\Users\doron> $counter = new-object Diagnostics.PerformanceCounter
PS C:\Users\doron> $counter.CategoryName = "My category"
PS C:\Users\doron> $counter.CounterName = "My counter name"
PS C:\Users\doron> $counter.ReadOnly = 1
PS C:\Users\doron> $counter


CategoryName     : My category
CounterHelp      : My counter name
CounterName      : My counter name
CounterType      : NumberOfItems64
InstanceLifetime : Global
InstanceName     :
ReadOnly         : True
MachineName      : .
RawValue         : 0
Site             :
Container        :



PS C:\Users\doron> $counter.ReadOnly = 0
PS C:\Users\doron> $counter


CategoryName     : My category
CounterHelp      : My counter name
CounterName      : My counter name
CounterType      : NumberOfItems64
InstanceLifetime : Global
InstanceName     :
ReadOnly         : False
MachineName      : .
RawValue         : 20
Site             :
Container        :



PS C:\Users\doron> $counter.ReadOnly = 1
PS C:\Users\doron> $counter


CategoryName     : My category
CounterHelp      : My counter name
CounterName      : My counter name
CounterType      : NumberOfItems64
InstanceLifetime : Global
InstanceName     :
ReadOnly         : True
MachineName      : .
RawValue         : 0
Site             :
Container        :

Server is Windows 2008 R2 running under .NET 4.5.

, , ( , ). RawValue , ReadOnly .

, ?

+5
1

, , .

RawValue, ReadOnly=True vs Readonly=False

MSDN:

, , , RawValue . NextSample.

: http://msdn.microsoft.com/de-de/library/system.diagnostics.performancecounter.rawvalue.aspx

, , RawValue:

public long RawValue
{
  get
  {
    if (this.ReadOnly)
      return this.NextSample().RawValue;
    this.Initialize();
    return this.sharedCounter.Value;
  }
  ...
}

MSDN , , ReadOnly False, , , , Initialize, sharedCounter Initialize. Initialize , .

, , , , readonly, .

, , , , - , , RawValue 0 , .

, ReadOnly True RawValue, , PerformanceCounter .

NextSample() RawValue. , .

, !

+5

All Articles