To create a new category and set of performance counters programmatically:
// Create a collection of type CounterCreationData
var collection = new CounterCreationDataCollection();
// Create the counter and set its properties.
var data = new CounterCreationData("Number Of Items", "...", PerformanceCounterType.NumberOfItems32);
// Add counter to the collection.
collection.Add(data);
// Create the category and pass the collection to it.
PerformanceCounterCategory.Create(
"DimecastDeme",
"",
PerformanceCounterCategoryType.MultiInstance,
collection);
// Get the counter already been created (Counter Name: Number of Items; Category: DimecastDeme)
var counter = new PerformanceCounter("DimecastDeme", "Number Of Items", "Demo", false);
// set value
counter.RawValue = 1;
counter.IncrementBy(2);
counter.IncrementBy(4);
var collection = new CounterCreationDataCollection();
// Create the counter and set its properties.
var data = new CounterCreationData("Number Of Items", "...", PerformanceCounterType.NumberOfItems32);
// Add counter to the collection.
collection.Add(data);
// Create the category and pass the collection to it.
PerformanceCounterCategory.Create(
"DimecastDeme",
"",
PerformanceCounterCategoryType.MultiInstance,
collection);
// Get the counter already been created (Counter Name: Number of Items; Category: DimecastDeme)
var counter = new PerformanceCounter("DimecastDeme", "Number Of Items", "Demo", false);
// set value
counter.RawValue = 1;
counter.IncrementBy(2);
counter.IncrementBy(4);
Reference:
http://msdn.microsoft.com/en-us/library/5e3s61wf.aspx
http://www.techscreencast.com/language/dotnet/how-to-create-a-custom-performance-counter/1856