Functions of performance counters
Application TracingProgramResource usage, operation efficiency, load and performance bottlenecks.
Add custom performance counters
Method 1: Use Visual StudioAdd performance counters.
OpenVisual Studio"Server Explorer"Panel, connect to the local computer, right-click "Performance counters"Group, select"Create new category"Opens the Add category window, such:
Here, you can set the category Name and description, create a counter, and set the type and description of the counter. ClickOKAfter the button is complete, you canPerformance counters"To view the custom performance counters you have added. You can also click"HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services"Node view.
Method 2: Add a performance counter by programming.
Main useSystem. Diagnostics. performancecountercategory. CreateMethod, which currently has4Reload versions, as shown below:
Public Static Performancecountercategory create ( String Categoryname, String Categoryhelp,
Countercreationdatacollection counterdata );
Public StaticPerformancecountercategory create (StringCategoryname,StringCategoryhelp,
Performancecountercategorytype categorytype, countercreationdatacollection counterdata );
Public static performancecountercategory create ( string categoryname, string categoryhelp,
string countername, string counterhelp );
Public StaticPerformancecountercategory create (StringCategoryname,StringCategoryhelp,
Performancecountercategorytype categorytype,StringCountername,StringCounterhelp );
The categorytype ParameterUsed to indicate that the category is a single instance (Single-instance), Multi-instance (Multi-instance) OrUnknownType. The default value is single instance.. NET Framework 1.0/1.1You do not need to specify this parameter.
DetailsCodeAs follows:
Using System;
Using System. diagnostics;
NamespaceMyperformancecounter
{
ClassProgram
{
Static VoidMain (String[] ARGs)
{
Createcounter ();
Console. Readline ();
}
Public Static VoidCreatecounter ()
{
Console. writeline ("Creating");
If ( ! Performancecountercategory. exists ( " Mycategory " ))
{
Performancecountercategory. Create ( " Mycategory " ,
" My category description. " ,
Performancecountercategorytype. singleinstance,
" Mycounter " ,
" This is my first custom memory Mace counter. " );
}
Else
{
Console. writeline ( " Mycategory already exists " );
}
Console. writeline ("Done");
}
}
}
Use performance counters in code
Here we useIncrementThe Code is as follows:
<ASP:Button ID= "Btnincrement" Runat= "Server" Onclick= "Btnincrement_click" Text="Add" />
Protected Void Btnincrement_click ( Object Sender, eventargs E)
{
Performancecounter counter = New Performancecounter ();
Counter. categoryname = " Mycategory " ;
Counter. countername = " Mycounter " ;
Counter. readonly = False ;< P>
counter. increment ();
counter. close ();
}< br>
Monitoring performance counters
open administrative tools ( Administrative Tools ) in the Performance Monitor to add a custom counter to the monitor, click the Add button on the Web page. The counter value in the monitor also increases.
reference : . net Framework performance counters (msdn )