您的位置:首页 > 其它

利用Attribute实现对性能计数器的封装

2008-12-09 10:17 218 查看
本文装载自:http://it.dianping.com/using_attribute_wrapping_performance_counter.htm

为要使用性能计数器对程序做监控,所以去MSDN查了一下性能计数器的实现,看完第一个感觉就是。。好麻烦。。如果要用计数器,要实现以下几步:

新建计数器分类

新建计数器集

新建计数器

创建计数器实例

初始化计数器实例

使用计数器实例

我想如果对于开发人员来说,为了实现监控,要写那么一大坨初始化代码,肯定晕倒。。所以有必要对性能计数器的实现做一下封装,让开发变得更加方便简单。

网上也看了很多种计数器的封装实现,大多是用工厂模式进行的封装,但感觉还是会有一定的冗余代码。于是尝试了一下使用Attribute方式进行封装,感觉效果还不错,所以和大家分享一下。

我预期的效果是这样的,首先是定义计数器集:

public static bool Register<T>() where T : class, new()

{

//取得category名字

object[] attribs = typeof(T).GetCustomAttributes(typeof(CounterCategoryAttribute), false);

if (attribs.Length == 0) return false;

CounterCategoryAttribute attr = (CounterCategoryAttribute)attribs[0];

string category = attr.Name;

//检查category是否已存在

bool countersExist = PerformanceCounterCategory.Exists(category);

//如果不存在,需要创建

if (countersExist == false)

{

//创建一个计数器集

CounterCreationDataCollection list = new CounterCreationDataCollection();

foreach (FieldInfo prop in typeof(T).GetFields())

{

attribs = prop.GetCustomAttributes(typeof(CounterUnitAttribute), false);

foreach (CounterUnitAttribute fieldAttrib in attribs)

{

//创建计数器

CounterCreationData data = new CounterCreationData();

data.CounterName = fieldAttrib.Name;

data.CounterHelp = fieldAttrib.Help;

data.CounterType = fieldAttrib.Type;

//加入计数器集

list.Add(data);

}

}

//创建category和计数器集

PerformanceCounterCategory.Create(category, "",  PerformanceCounterCategoryType.SingleInstance, list);

}

//创建并绑定计数器实例

T instance = new T();

foreach (FieldInfo prop in typeof(T).GetFields())

{

attribs = prop.GetCustomAttributes(typeof(CounterUnitAttribute), false);

foreach (CounterUnitAttribute fieldAttrib in attribs)

{

PerformanceCounter pc = new PerformanceCounter(category, fieldAttrib.Name, "",false);

pc.RawValue = 0;

prop.SetValue(instance, pc);

}

}

//存入hash表中,供Resolve函数读取

_cntList.Add(typeof(T),instance);

return true;

}

通过这样的封装,大部分的重复代码都被包起来了,使用起来会方便很多。具体的代码我附在参考资料里,供大家学习研究,望多提宝贵意见:)

本文转载自:http://it.dianping.com/using_attribute_wrapping_performance_counter.htm

参考资料下载: http://it.dianping.com/using_attribute_wrapping_performance_counter.htm

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: