繁体   English   中英

递增自定义性能计数器 .net 4.0 Windows 7

[英]Incrementing Custom Performance Counters .net 4.0 Windows 7

我在 Windows 7 上增加自定义性能计数器时遇到了一点问题。创建了类别,创建了计数器,但是调用“Increment()”或设置“RawValue”没有任何效果。 例如,Increment() 的返回值保持为 0L。

以下代码是一个控制台应用程序,需要以管理员身份运行才能创建类别。 调试通过显示上述症状。

任何帮助是极大的赞赏。

class Program
{
    private static string _category = "MyCustomCounters";

    static void Main(string[] args)
    {
        var deleteIfExists = false;

        if (args.Any())
        {
            if (args.Count() > 0)
            {
                _category = args[0];
            }

            if (args.Count() > 1)
            {
                deleteIfExists = args[1].ToUpper() == bool.TrueString.ToUpper();
            }
        }

        CreateCustomCounters(_category, deleteIfExists);
    }

    private static void CreateCustomCounters(string category, bool deleteIfExists)
    {
        if (deleteIfExists && PerformanceCounterCategory.Exists(category))
        {
            PerformanceCounterCategory.Delete(category);
        }

        if (!PerformanceCounterCategory.Exists(category))
        {
            CounterCreationDataCollection counterCollection = new CounterCreationDataCollection();

            // add a counter tracking operations per second
            CounterCreationData opsPerSec = new CounterCreationData();
            opsPerSec.CounterName = "# requests /sec";
            opsPerSec.CounterHelp = "Number of requests executed per second";
            opsPerSec.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
            counterCollection.Add(opsPerSec);

            // add a counter tracking operations per second
            CounterCreationData operationTotal = new CounterCreationData();
            operationTotal.CounterName = "Total # requests";
            operationTotal.CounterHelp = "Total number of requests executed";
            operationTotal.CounterType = PerformanceCounterType.NumberOfItems32;
            counterCollection.Add(operationTotal);

            PerformanceCounterCategory.Create(category,
                        "A custom counter category that tracks execution", PerformanceCounterCategoryType.SingleInstance,
                        counterCollection);
        }
        else
        {
            Console.Error.WriteLine("Counter already exists, try specifying parameters for categoryname and or insisting on deleting");
        }

        using (var _totalOperationsCounter = new PerformanceCounter(_category, "Total # requests", false))
        {
            _totalOperationsCounter.ReadOnly = false;
            _totalOperationsCounter.RawValue = 10;
            var count = _totalOperationsCounter.Increment();
        }
    }
}

对我来说,行为如下(我没有设置 RawValue)

  • 当性能监视器未运行时
    • Console.WriteLine(_totalOperationsCounter.Increment())始终显示 1
  • 当性能监视器正在运行时
    • Console.WriteLine(_totalOperationsCounter.Increment())应用程序运行之间的增量

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM