c#实现自定义window performance counter

下面是一个简单的实现 操作自定义window performance counter的实例。在运行程序的过程中我们通过操作系统的performance面板查看或者写log文件,对我们应用程序的性能进行监视,分析。将有助于我们分析解决系统的性能等问题。

 

代码功能: 

1,实现添加一个counter 类别

2,添加一个或者多个counter对象

3,获取counter对象,并赋值。

 

在系统的performance 面板中查看。效果如下图:

 

代码:

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Threading;
using  System.Diagnostics;

namespace  WritePerformanceLog
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            
if  ( ! PerformanceCounterCategory.Exists( " test_Category_1 " ))
            {
                
//  Create a collection of type CounterCreationDataCollection.
                System.Diagnostics.CounterCreationDataCollection CounterDatas  =
                   
new  System.Diagnostics.CounterCreationDataCollection();
                
//  Create the counters and set their properties.
                System.Diagnostics.CounterCreationData cdCounter1  =
                   
new  System.Diagnostics.CounterCreationData();
                System.Diagnostics.CounterCreationData cdCounter2 
=
                   
new  System.Diagnostics.CounterCreationData();
                cdCounter1.CounterName 
=   " Counter1 " ;
                cdCounter1.CounterHelp 
=   " help string1 " ;
                cdCounter1.CounterType 
=  System.Diagnostics.PerformanceCounterType.NumberOfItems64;
                cdCounter2.CounterName 
=   " Counter2 " ;
                cdCounter2.CounterHelp 
=   " help string 2 " ;
                cdCounter2.CounterType 
=  System.Diagnostics.PerformanceCounterType.NumberOfItems64;

                
                
//  Add both counters to the collection.
                CounterDatas.Add(cdCounter1);
                CounterDatas.Add(cdCounter2);
                
//  Create the category and pass the collection to it.
                System.Diagnostics.PerformanceCounterCategory.Create(
                   
" test_Category_1 " " Category help " , CounterDatas);
            }
            
else
            {

                PerformanceCounter cdCounter1 
=   new  PerformanceCounter( " test_Category_1 " " Counter1 " false );
                PerformanceCounter cdCounter2 
=   new  PerformanceCounter( " test_Category_1 " " Counter2 " false );
                
                cdCounter1.ReadOnly 
=   false ;

                
for int  i = 0 ;i < 10000 ;i ++  )
                {
                    cdCounter1.RawValue 
=  i;
                    
// cdCounter1.Increment();

                    cdCounter2.RawValue 
=  i + 1 ;

                    Thread.Sleep(
100 );
                }
                                           

                Console.WriteLine(cdCounter1.NextValue());
                Console.WriteLine(cdCounter2.RawValue);
                Console.WriteLine(cdCounter1.NextSample());


                Console.Read();

            }
        }
    }
}

转载于:https://www.cnblogs.com/luyinghuai/archive/2008/08/21/1272662.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值