Loaded from: http://it.dianping.com/using_attribute_wrapping_performance_counter.htm
To use a performance counter pairProgramFor monitoring, I went to msdn to check the implementation of the Performance Counter. After reading this, I felt like .. Very troublesome .. To use a counter, perform the following steps:
- New counter category
- Create counter set
- New counter
- Create a counter instance
- Initialize counter instance
- Use counter instances
I think if developers need to write a large amount of initialization to implement monitoringCode, You must have fainted .. Therefore, it is necessary to encapsulate the implementation of performance counters to make development easier.
I also read many counter encapsulation implementations on the internet, mostly in factory mode, but I still feel that there will be some redundant code. So I tried to use the attribute Method for encapsulation, and it seems that the effect is not bad. So I 'd like to share it with you.
The expected result is as follows: first, define the counter set:
Code
[Countercategory ( " Test " )]
Class Xcounters
{
[Counterunit ( " X " , " Value x " , Performancecountertype. numberofitems64)]
Public Performancecounter X;
[Counterunit ("Y","Value Y", Performancecountertype. rateofcountspersecond64)]
PublicPerformancecounter y;
}
Through attribute, I want to achieve the above results, that is, you can define the counter category name, and the name, description, and counter type of each counter under the category.
Then use:
Code
Countercontainer. Register < Xcounters > ();
Xcounters counter=Dpcountercontainer. Resolve<Xcounters>();
For(IntI= 0; I< 10000000; I++)
{
Counter. X. increment ();
Counter. Y. increment ();
Thread. Sleep (300);
}
The first sentence is to register xcounter in the container. The container is responsible for classifying and binding the counters defined in xcounter.
The second sentence is to obtain the xcounter counter set instance from the iner.
The third and fourth sentences are the use of counters. Add 1 to the X and Y counters every 0.3 seconds. The final running result is as follows:
After encapsulation, developers need to pay attention to the definition and use of the counter set, rather than the disgusting initialization code.
Then, how does the container implement the above results? Let's continue to discuss:
In fact, the most important thing is the reginer's register function. Please refer to the implementation:
Code
Public Static Bool Register < T > () Where T: Class , New ()
{
// Get category name
Object [] Attribs = Typeof (T). getcustomattributes ( Typeof (Countercategoryattribute ), False );
If (Attribs. Length = 0 ) Return False ;
Countercategoryattribute ATTR = (Countercategoryattribute) attribs [ 0 ];
String Category = ATTR. Name;
//Check whether the category already exists
BoolCountersexist=Performancecountercategory. exists (category );
//If not, create
If(Countersexist= False)
{
//Create a counter set
Countercreationdatacollection list= NewCountercreationdatacollection ();
Foreach (Fieldinfo prop In Typeof (T). getfields ())
{
Attribs = Prop. getcustomattributes ( Typeof (Counterunitattribute ), False );
Foreach (Counterunitattribute fieldattrib In Attribs)
{
// Create a counter
Countercreationdata data = New Countercreationdata ();
Data. countername = Fieldattrib. Name;
Data. counterhelp = Fieldattrib. help;
Data. countertype = Fieldattrib. type;
//Add counter set
List. Add (data );
}
}
//Create category and counter set
Performancecountercategory. Create (category,"", Performancecountercategorytype. singleinstance, list );
}
// Create and bind a counter instance
T instance = New T ();
Foreach (Fieldinfo prop In Typeof (T). getfields ())
{
Attribs = Prop. getcustomattributes ( Typeof (Counterunitattribute ), False );
Foreach (Counterunitattribute fieldattrib In Attribs)
{
Performancecounter PC = New Performancecounter (category, fieldattrib. Name, "" , False );
PC. rawvalue = 0 ;
Prop. setvalue (instance, PC );
}
}
// Store the data in the hash table for the resolve function to read.
_ Cntlist. Add ( Typeof (T), instance );
Return True;
}
With such encapsulation, most of the repeated code is encapsulated, which makes it much easier to use. The specific code is included in the reference materials for your study. I hope to give more valuable comments :)
This article Reprinted from: http://it.dianping.com/using_attribute_wrapping_performance_counter.htm
Reference download: http://it.dianping.com/using_attribute_wrapping_performance_counter.htm