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

 

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

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

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值