Cannot create instance for performance counter

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi to all

I was trying to create a performance counter instance, but I can see the counter but it does not have any instance. Do you know what is the problem

// Delete performance counter category if this exist
if (PerformanceCounterCategory.Exists("Test Counters")

PerformanceCounterCategory.Delete("Test Counters")


// Create a new Test counte
CounterCreationData performanceDataCounter = new CounterCreationData()
performanceDataCounter.CounterType = PerformanceCounterType.NumberOfItems32
performanceDataCounter.CounterName = "Test counter"
performanceDataCounter.CounterHelp = "Contains test counters."

CounterCreationDataCollection countersCollection = new CounterCreationDataCollection()
countersCollection.Add(performanceDataCounter)
PerformanceCounterCategory.Create("Test Counters", "Test performance counters", countersCollection)

// Initialize counter
PerformanceCounter pcPerformanceDataCounters = new PerformanceCounter("Test Counters", "Test counter", "Total", false)

// Set first valu
pcPerformanceDataCounters.RawValue = 0

// Create another read-only counter to verify the previous counte
PerformanceCounter pcPerformanceDataCountersValidator = new PerformanceCounter("Test Counters", "Test counter", "_Total")
Console.WriteLine("Set performance counter value: " + pcPerformanceDataCountersValidator.NextSample().ToString())

When I run this application I get the exception message "Category does not exist." when I instantiate the pcPerformanceDataCountersValidator object (This is because the previous instance counter was not created)

Thanks for your time . . .
 
Sorry, There was a typo in the original message, here is the code (It still fails, the counter is created but has no instance)

// Delete performance counter category if this exist
if (PerformanceCounterCategory.Exists("Test Counters")

PerformanceCounterCategory.Delete("Test Counters")


// Create a new Test counte
CounterCreationData performanceDataCounter = new CounterCreationData()
performanceDataCounter.CounterType = PerformanceCounterType.NumberOfItems32
performanceDataCounter.CounterName = "Test counter"
performanceDataCounter.CounterHelp = "Test counter."

CounterCreationDataCollection countersCollection = new CounterCreationDataCollection()
countersCollection.Add(performanceDataCounter)
PerformanceCounterCategory.Create("Test Counters", "Test performance counters", countersCollection)

// Initialize counter
PerformanceCounter pcPerformanceDataCounters = new PerformanceCounter("Test Counters", "Test counter", "Total", false)

// Set first valu
pcPerformanceDataCounters.RawValue = 0

// Create another read-only counter to verify the previous counte
PerformanceCounter pcPerformanceDataCountersValidator = new PerformanceCounter("Test Counters", "Test counter", "Total")
Console.WriteLine("Set performance counter value: " + pcPerformanceDataCountersValidator.NextSample().ToString())

Again, Thanks in advance for your time . .

----- Jeanmark wrote: ----

Hi to all

I was trying to create a performance counter instance, but I can see the counter but it does not have any instance. Do you know what is the problem

// Delete performance counter category if this exist
if (PerformanceCounterCategory.Exists("Test Counters")

PerformanceCounterCategory.Delete("Test Counters")


// Create a new Test counte
CounterCreationData performanceDataCounter = new CounterCreationData()
performanceDataCounter.CounterType = PerformanceCounterType.NumberOfItems32
performanceDataCounter.CounterName = "Test counter"
performanceDataCounter.CounterHelp = "Contains test counters."

CounterCreationDataCollection countersCollection = new CounterCreationDataCollection()
countersCollection.Add(performanceDataCounter)
PerformanceCounterCategory.Create("Test Counters", "Test performance counters", countersCollection)

// Initialize counter
PerformanceCounter pcPerformanceDataCounters = new PerformanceCounter("Test Counters", "Test counter", "Total", false)

// Set first valu
pcPerformanceDataCounters.RawValue = 0

// Create another read-only counter to verify the previous counte
PerformanceCounter pcPerformanceDataCountersValidator = new PerformanceCounter("Test Counters", "Test counter", "_Total")
Console.WriteLine("Set performance counter value: " + pcPerformanceDataCountersValidator.NextSample().ToString())

When I run this application I get the exception message "Category does not exist." when I instantiate the pcPerformanceDataCountersValidator object (This is because the previous instance counter was not created)

Thanks for your time . . .
 
I am trying to get a web service performance counter to work and I get
the same error -

System.InvalidOperationException: Category does not exist.
at System.Diagnostics.PerformanceCounterLib.CounterExists(String
machine, String category, String counter)
at System.Diagnostics.PerformanceCounter.Initialize()
at System.Diagnostics.PerformanceCounter..ctor(String categoryName,
String counterName, String instanceName, Boolean readOnly)
at System.Diagnostics.PerformanceCounter..ctor(String categoryName,
String counterName, String instanceName)
at PerfMon.PerfCounters.Poll(Int32 counterType, String counter) in
c:\inetpub\wwwroot\PerfMon\PerfMon.asmx.vb:line 36
at PerfMon.PerfCounters.GetCounters() in
c:\inetpub\wwwroot\PerfMon\PerfMon.asmx.vb:line 18
 
Back
Top