C # traverse all performance counters performancecounter

Source: Internet
Author: User

 

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 ().

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.