如何创建自定义性能计数器

 

为什么要创建自定义的性能计数器?

You create categories and custom performance counters if you want to track data that is not captured by the standard counters provided with Microsoft Windows. For example, you might create a custom counter to track the total number of users logged on to your Web site, or to track the orders processed per second by your site. When you create a counter, you add it to a performance counter category, and you assign it a type that governs how it will behave.

参考:

http://msdn.microsoft.com/zh-cn/library/ty9fywea(v=VS.71).aspx

 

 

创建自定义的性能计数器的方法:

Creating a Single Performance Counter Using PerformanceCounterCategory

 

1. Create a new text file named CreateCounter.cs and add the following code.

// CreateCounter.cs

using System;

using System.Diagnostics;

public class CustomCounter

{

public static void Main()

{

Console.WriteLine("Creating custom counter");

CreateCounter();

Console.WriteLine("Done");

Console.ReadLine();

}

public static void CreateCounter()

{

if (!PerformanceCounterCategory.Exists("MySingleCategory"))

{

PerformanceCounterCategory.Create ("MySingleCategory",

"My New Perf Category Description", "MyCounter",

"My New Perf Counter Desc");

}

else

{

Console.WriteLine("Counter already exists");

}

}

}

2. Compile the code using the following command line.

csc.exe /out:CreateCounter.exe /t:exe /r:system.dll CreateCounter.cs

3. Run CreateCounter.exe from a command prompt to create your new performance

counter.

CreateCounter.exe

Validating Your Performance Counter Category and Performance Counter

Use Regedt32.exe to verify that your performance counter category and your custom performance counter are created in the following registry folder.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services

The performance counter category is named MySingleCategory, and the performance counter is named MyCounter.

 

Using Server Explorer

If you have Visual Studio .NET, you can create a custom performance counter manually by using Server Explorer.

_ To create a custom performance counter with Server Explorer

1. Open Server Explorer and connect to your local server.

2. Right-click Performance Counters and select Create New Category.

3. Enter MyCategory in the category name box and optionally enter a description in the Performance Counter Builder box.

4. Click New at the bottom of the form and enter MyCounter in the counter name box.

5. Click OK.

 

Calling PerformanceCounter.Increment

The following ASP.NET page shows you how to use your custom counter. This code calls PerformanceCounter.Increment to set the new counter value.

IncrementCounter.aspx

<%@ language=C# %>

<%@ import namespace="System.Diagnostics" %>

<script runat=server>

void IncrementCounter(Object sender, EventArgs e)

{

// get an instance of our perf counter

PerformanceCounter counter = new PerformanceCounter();

counter.CategoryName = "mySingleCategory";

counter.CounterName = "myCounter";

counter.ReadOnly = false;

// increment and close the perf counter

counter.Increment();

counter.Close();

Response.Write("Counter is incremented");

}

</script>

<form runat=server>

<input type="submit" id="btnSubmit" Value="Increment Counter"

OnServerClick="IncrementCounter" runat=server />

</form>

 

 

参考:

1、《Improving .NET Application Performance and Scalability》

2、http://msdn.microsoft.com/zh-cn/library/cc437982(v=vs.71).aspx

 

 

 

posted on 2011-01-12 15:18  preftest  阅读(917)  评论(2编辑  收藏  举报

导航