Use custom performance counters in ASP. NET

Source: Internet
Author: User

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 )

Related Article

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.