【问题标题】:RawFraction performance counter persists its state even after deleting the performance categoryRawFraction 性能计数器即使在删除性能类别后仍保持其状态
【发布时间】: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


    【解决方案1】:

    我很确定这是由于 Windows 管理性能数据的方式造成的。

    来自 MSDN,PerformanceCounterCategory.Create Method (String, String, PerformanceCounterCategoryType, CounterCreationDataCollection)

    注意 强烈推荐新的性能计数器 类别是在安装应用程序期间创建的,而不是 在应用程序执行期间。这允许时间 操作系统刷新其注册性能计数器列表 类别。如果列表尚未刷新,则尝试使用 类别将失败。

    我没有第一手的知识,但这表明添加或删除类别不是同步操作。

    要解决此问题,您可能希望将第一个 if 替换为 while,如下所示:

    while (PerformanceCounterCategory.Exists(category))
    {
        PerformanceCounterCategory.Delete(category);
    }
    

    不过,这有点笨拙。最好的建议是不要在需要之前设置或拆除计数器。相反,将其放入安装程序中,或者至少创建一个单独的工具来安装/卸载它们。此外,您可以创建一个 Powershell 脚本来安装/卸载它们。示例见http://msdn.microsoft.com/en-us/library/windowsazure/hh508994.aspx

    【讨论】:

      猜你喜欢
      • 2010-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-04
      • 2011-12-31
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多