• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#实现自定义windowperformancecounter

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

下面是一个简单的实现 操作自定义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;

                
forint 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();

            }
        }
    }
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap