C # traverse all performance counters performancecounter
In C #, the type of the performance counter is performancecounter. When using this type, you need to know the categoryname and countername of the counter. For Commonly Used processor, network, and so on, there are still many references for reference query. If it is a counter of a strange point, simply look for the categoryname, countername and InstanceName of the counter.
The following provides a method to traverse all counter information on the local machine.
Performancecountercategory [] PCC = performancecountercategory. getcategories (); stringbuilder sb = new stringbuilder (); string Cn = NULL; For (INT I = 0; I <PCC. length; I ++) {Cn = PCC [I]. categoryname. toupper (); // If (CN. indexof ("processor ")! =-1) {sb. remove (0, sb. length); sb. append ("categoryname:" + PCC [I]. categoryname + "\ r \ n"); sb. append ("machinename:" + PCC [I]. machinename + "\ r \ n"); string [] instancenames = PCC [I]. getinstancenames (); For (Int J = 0; j <instancenames. length; j ++) {sb. append ("***** Instance name *********** \ r \ n"); sb. append ("instancename:" + instancenames [J] + "\ r \ n"); try {cececounter [] counters = PCC [I]. getcounters (instancenames [J]); For (int K = 0; k <counters. length; k ++) {sb. append ("countername:" + counters [K]. countername + "\ r \ n") ;}} catch (exception) {} sb. append ("************************************* * ************ \ r \ n ");} trace. traceinformation (sb. tostring ());}}
In addition, it is easy to use this type to obtain the value of a counter. You only need to specify the category, Instance name, and counter name.
Performancecounter pC1 = new performancecounter ("processor", "% processor time", "_ total"); // The parameters are: Class Name, counter name, and Instance name.
Then you can obtain the counter value through pc1.nextvalue ().