【发布时间】:2012-01-11 19:38:16
【问题描述】:
我正在正确创建和设置性能计数器,但是当我删除类别、重新创建具有相同名称的类别并将计数器添加/更新到该类别时,它无法更新计数器及其值。
以下代码第一次运行良好,但第二次运行良好。现在不需要删除“删除类别”的代码,但我希望能够在每次部署应用程序时删除现有类别。
如果计数器不这样做或重置其值,我该如何永久删除它?
private PerformanceCounter mainCounter;
private PerformanceCounter mainCounterBase;
private string category = "TestPerformanceCounterTest";
public void Test()
{
//Counter setup
if (PerformanceCounterCategory.Exists(category))
PerformanceCounterCategory.Delete(category);
if (!PerformanceCounterCategory.Exists(category))
{
var categoryCollection = new CounterCreationDataCollection();
var counter1 = new CounterCreationData("RawCounter1", "", PerformanceCounterType.RawFraction);
var counter2 = new CounterCreationData("RawCounterBase1", "", PerformanceCounterType.RawBase);
categoryCollection.Add(counter1);
categoryCollection.Add(counter2);
PerformanceCounterCategory.Create(category, "", PerformanceCounterCategoryType.SingleInstance, categoryCollection);
// Wait and wait...
Thread.Sleep(TimeSpan.FromSeconds(3));
}
//create counters
mainCounter = new PerformanceCounter(category, "RawCounter1", false);
mainCounterBase = new PerformanceCounter(category, "RawCounterBase1", false);
//reset values
mainCounter.RawValue = 0;
mainCounterBase.RawValue = 0;
//update counter
mainCounter.IncrementBy(10);
mainCounterBase.IncrementBy(20);
**Console.WriteLine("Main counter: " +mainCounter.RawValue);//doesnt show value 50 the second time this is run**
Console.WriteLine("Main counter Base: " + mainCounterBase.RawValue);
Console.WriteLine("Main counter next value: " + mainCounter.NextValue());
Console.WriteLine("Main counter base next value: " + mainCounterBase.NextValue());
}
【问题讨论】:
标签: c# performance counter performancecounter