PerformanceCounters

  • Thread starter Thread starter alien2_51
  • Start date Start date
A

alien2_51

I'm having issues getting some custom performance counters to work.. Here's
what I'm trying to do...

I want to create one category with two CountPerTimeInterval32 counters,
which represent an interface.. For each implementation I want to add a new
instance for both counters so each implementation can be tracked
seperately.. The category and counters (minus the instances) show up in the
in the performance object, although the bases do not. When I try to
increment the counters nothing shows up, here is my implementation, please
help...

TIA,

Dan B
Imports System

Imports System.Collections

Imports System.Collections.Specialized

Imports System.Diagnostics

Imports MNCAppServices

Public Class Collector

Private pcCategory As PerformanceCounterCategory

Private pcBDOGet As PerformanceCounter

Private pcBDOGetBase As PerformanceCounter

Private pcBDOSave As PerformanceCounter

Private pcBDOSaveBase As PerformanceCounter

Sub New()

SetupCategory()

End Sub

Private Function SetupCategory() As Boolean

Try

If Not PerformanceCounterCategory.Exists("MNC BDO Statistics") Then

Dim CCDC As New CounterCreationDataCollection

'create counters

Dim cptBDOGet As New CounterCreationData("BDOGet", "Tracks calls to BDOGet
by time interval.", PerformanceCounterType.CountPerTimeInterval32)

CCDC.Add(cptBDOGet)

Dim cptBDOGetBase As New CounterCreationData("BDOGetBase", "Tracks calls to
BDOGet by time interval.", PerformanceCounterType.AverageBase)

CCDC.Add(cptBDOGetBase)

Dim cptBDOSave As New CounterCreationData("BDOSave", "Tracks calls to
BDOSave by time interval.", PerformanceCounterType.CountPerTimeInterval32)

CCDC.Add(cptBDOSave)

Dim cptBDOSaveBase As New CounterCreationData("BDOSaveBase", "Tracks calls
to BDOGet by time interval.", PerformanceCounterType.AverageBase)

CCDC.Add(cptBDOSaveBase)

' Create the category.

PerformanceCounterCategory.Create("MNC BDO Statistics", "Collects statistics
about Business Data Object Usage.", CCDC)

CreateCounters()

Return True

Else

GetCategoryRef()

GetCountersRef()

Return True

End If

Catch ex As Exception

Utility.LogException(ex)

Return False

End Try

End Function 'SetupCategory

Private Sub CreateCounters()

' Create the counters.

pcBDOGet = New PerformanceCounter("MNC BDO Statistics", "BDOGet",
"CustomerContact", False)

pcBDOGet.ReadOnly = False

pcBDOGet.RawValue = 0

pcBDOGetBase = New PerformanceCounter("MNC BDO Statistics", "BDOGetBase",
"CustomerContact", False)

pcBDOGetBase.ReadOnly = False

pcBDOGetBase.RawValue = 0

pcBDOSave = New PerformanceCounter("MNC BDO Statistics", "BDOSave",
"CustomerContact", False)

pcBDOSave.ReadOnly = False

pcBDOSave.RawValue = 0

pcBDOSaveBase = New PerformanceCounter("MNC BDO Statistics", "BDOSaveBase",
"CustomerContact", False)

pcBDOSaveBase.ReadOnly = False

pcBDOSaveBase.RawValue = 0

End Sub 'CreateCounters

Private Sub GetCountersRef()

' Create references to counters.

Dim objCnt As PerformanceCounter

For Each objCnt In pcCategory.GetCounters

Select Case objCnt.CounterName

Case Is = "BDOGet"

pcBDOGet = objCnt

pcBDOGet.ReadOnly = False

Case Is = "BDOGetBase"

pcBDOGetBase = objCnt

pcBDOGetBase.ReadOnly = False

Case Is = "BDOSave"

pcBDOSave = objCnt

pcBDOSave.ReadOnly = False

Case Is = "BDOSaveBase"

pcBDOSaveBase = objCnt

pcBDOSaveBase.ReadOnly = False

End Select

Next

End Sub 'CreateCounters

Private Sub GetCountersRef(ByVal InstanceName As String)

' Create references to counters.

Dim objCnt As PerformanceCounter

For Each objCnt In pcCategory.GetCounters(InstanceName)

Select Case objCnt.CounterName

Case Is = "BDOGet"

pcBDOGet = objCnt

pcBDOGet.ReadOnly = False

Case Is = "BDOGetBase"

pcBDOGetBase = objCnt

pcBDOGetBase.ReadOnly = False

Case Is = "BDOSave"

pcBDOSave = objCnt

pcBDOSave.ReadOnly = False

Case Is = "BDOSaveBase"

pcBDOSaveBase = objCnt

pcBDOSaveBase.ReadOnly = False

End Select

Next

End Sub

Private Sub GetCategoryRef()

Dim objCategory As PerformanceCounterCategory

For Each objCategory In PerformanceCounterCategory.GetCategories

If objCategory.CategoryName = "MNC BDO Statistics" Then

pcCategory = objCategory

End If

Next

End Sub

Public Sub IncrementSaveCounter(ByVal InstanceName As String)

If pcCategory.InstanceExists(InstanceName) Then

GetCountersRef(InstanceName)

pcBDOSave.Increment()

End If

End Sub

Public Sub IncrementGetCounter(ByVal InstanceName As String)

If pcCategory.InstanceExists(InstanceName) Then

GetCountersRef(InstanceName)

pcBDOGet.Increment()

End If

End Sub

End Class 'App
 
Hi Dan,

I've left this for a couple of days hoping that someone would come through
for you.

I tried your code on my system but got stopped at
PerformanceCounterCategory.Create()
by this:

!! A first chance exception of type 'System.InvalidOperationException'
!! occurred in system.dll
!!
!! Additional information: The Counter layout for the Category specified is
!! invalid, a counter of the type: AverageCount64, AverageTimer32,
!! CounterMultiTimer, CounterMultiTimerInverse, CounterMultiTimer100Ns,
!! CounterMultiTimer100NsInverse, RawFraction, SampleFraction or
!! SampleCounter has to be immediately followed by any of the base counter
!! types : AverageBase, MultiBase, RawBase or SampleBase.

That prevented me from any further investigation, I'm afraid.

Did you set anything up manually beforehand ? What does your system know
that mine doesn't, I wonder. (I'm running v2002, .NET 1.0.)

Regards,
Fergus
 
Thanks for your reply,

I too recieved that same error I was able to rectify the problem by adding a
call like so:

This call sets up the counter:
Dim cptBDOGet As New CounterCreationData("BDOGet", "Tracks calls to BDOGet
by time interval.", PerformanceCounterType.CountPerTimeInterval32)

CCDC.Add(cptBDOGet)

This call sets up the base counter, whatever that is...

Dim cptBDOGetBase As New CounterCreationData("BDOGetBase", "Tracks calls to
BDOGet by time interval.", PerformanceCounterType.AverageBase)

CCDC.Add(cptBDOGetBase)

After doing this it seemed to setup the counter correctly....

I think my main problem here was I was always creating new counter rather
than getting a reference to the existing ones, this is why I figured my
counters never had data, because the were always new...
 
Back
Top