streda 26. júna 2013

Custom system performance counter in C#

Hello everyone

this is simple example how to create and use your own system performance counters in C#.



Code Snippet
1.  class Program
2.  {
3.      private static string categoryName = "TestingCategory";
4.      private static string counterName = "Loops per second";
5.   
6.      static void Main(string[] args)
7.      {
8.          CreateCounter();
9.   
10.        UseCounter();
11. 
12.    }
13. 
14.    private static void UseCounter()
15.    {
16.        using (PerformanceCounter counter = new PerformanceCounter(categoryName, counterName, ""))  //Instance name is empty string.
17.        {
18.            counter.ReadOnly = false;
19.            for (int i = 0; i < 100000000; i++)
20.                counter.Increment();
21.            //counter.IncrementBy(10);              
22.        }
23.    }
24. 
25.    private static void CreateCounter()
26.    {
27. 
28.        //Test if category already exists
29.        if (!PerformanceCounterCategory.Exists(categoryName))
30.        {
31.            //Prepare and create counter
32.            CounterCreationDataCollection counter = new CounterCreationDataCollection();
33.            counter.Add(new CounterCreationData(counterName, "For loop counter", PerformanceCounterType.CountPerTimeInterval32));
34. 
            





Žiadne komentáre:

Zverejnenie komentára