📄 enterprisecounters.cs
字号:
using System;
using System.Reflection;
using System.Collections;
using System.Diagnostics;
namespace EnterpriseObjects
{
/// <summary>
/// Summary description for Counters.
/// </summary>
public class EnterpriseCounters
{
// members...
public string PerformanceObjectName = "MyEnterpriseApplication";
private EnterpriseCounterCollection _counters = new EnterpriseCounterCollection();
private ArrayList _counterCapableObjects = new ArrayList();
public EnterpriseCounters()
{
//
// TODO: Add constructor logic here
//
}
public EnterpriseCounterCollection Counters
{
get
{
return _counters;
}
}
public void ScanAssembly(object seedObject)
{
ScanAssembly(seedObject.GetType().Assembly);
}
public void ScanAssembly(Assembly scanAssembly)
{
// go through the types...
foreach(Type scanType in scanAssembly.GetTypes())
{
// get the attributes...
foreach(Type counterInterface in scanType.GetInterfaces())
{
// do we have one?
if(counterInterface == typeof(ICounterProvider))
{
// create it...
try
{
// call it and add it to the list...
ICounterProvider targetObject = (ICounterProvider)Activator.CreateInstance(scanType);
targetObject.CreateCounters(this);
_counterCapableObjects.Add(targetObject);
}
catch
{
}
}
}
}
}
public void CreateCounters()
{
CreateCounters(false);
}
public void CreateCounters(bool force)
{
// does the object exist?
bool countersExist = PerformanceCounterCategory.Exists(PerformanceObjectName);
// delete the category?
if(countersExist == true && force == true)
{
PerformanceCounterCategory.Delete(PerformanceObjectName);
countersExist = false;
}
// do we need to create it?
if(countersExist == false)
{
// create a collection...
CounterCreationDataCollection list = new CounterCreationDataCollection();
// go through each counter...
foreach(EnterpriseCounter counter in Counters)
{
// create some new data...
CounterCreationData data = new CounterCreationData();
data.CounterName = counter.Name;
data.CounterHelp = counter.HelpText;
data.CounterType = counter.Type;
// add it...
list.Add(data);
}
// create the category and all of the counters...
PerformanceCounterCategory.Create(PerformanceObjectName, "", list);
}
// now, go back through the counters and create instances...
foreach(EnterpriseCounter counter in Counters)
{
// create an instance and store it...
counter.Counter = new PerformanceCounter(PerformanceObjectName, counter.Name, "", false);
// reset the value...
counter.Counter.RawValue = 0;
}
// ok, now tell all the objects that registered counters that they have been created...
foreach(ICounterProvider counterObject in _counterCapableObjects)
counterObject.CountersCreated(this);
_counterCapableObjects.Clear();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -