需要使用 CounterDelta32 PerformanceCounter 的示例

发布于 2024-08-08 17:49:49 字数 545 浏览 2 评论 0原文

我试图显示自上次性能迭代以来操作发生的次数。我使用以下方法创建了一个性能计数器:

var clearStateCounterData = new CounterCreationData()
{
    CounterName = ClearStateName,
    CounterHelp = "The number of times the service state has been cleared since the last performance iteration",
    CounterType = PerformanceCounterType.CounterDelta32
};

然后我在应用程序中调用 counter.Increment() 但我从未看到性能计数器值移动。即使我每秒运行多次。

我是否需要一些特殊的东西或者需要增加一个特定值才能让 PerformanceCounter 显示某些内容?

弄清楚了

我在下面的答案中给出了使用此计数器的示例。谢谢你们的帮助。

I'm trying to show the number of times an operation occurred since the last performance iteration. I have created a performance counter using the following:

var clearStateCounterData = new CounterCreationData()
{
    CounterName = ClearStateName,
    CounterHelp = "The number of times the service state has been cleared since the last performance iteration",
    CounterType = PerformanceCounterType.CounterDelta32
};

Then I call counter.Increment() in my application but I never see the performance counter value move. Even if I run it multiple times per second.

Is there something special I need or a specific value I need to increment by to get the PerformanceCounter to show something?

Figured it out

I put an example of using this counter in an answer below. Thanks for the help guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔戏命师 2024-08-15 17:49:49

这是一个对我有用的例子。

class Program
{
    const string CategoryName = "____Test Category";
    const string CounterName = "Clear State Operations";

    static void Main(string[] args)
    {
        if (PerformanceCounterCategory.Exists(CategoryName))
            PerformanceCounterCategory.Delete(CategoryName);

        var counterDataCollection = new CounterCreationDataCollection();

        var clearStateCounterData = new CounterCreationData()
        {
            CounterName = CounterName,
            CounterHelp = "The number of times the service state has been cleared since the last performance iteration",
            CounterType = PerformanceCounterType.CounterDelta32
        };
        counterDataCollection.Add(clearStateCounterData);

        PerformanceCounterCategory.Create(CategoryName, "Test Perf Counters", PerformanceCounterCategoryType.SingleInstance, counterDataCollection);

        var counter = new PerformanceCounter(CategoryName, CounterName, false);

        for (int i = 0; i < 5000; i++)
        {
            var sw = Stopwatch.StartNew();
            Thread.Sleep(10300);
            sw.Stop();

            counter.Increment();
        }

        Console.Read();
    }
}

Here is an example that worked for me.

class Program
{
    const string CategoryName = "____Test Category";
    const string CounterName = "Clear State Operations";

    static void Main(string[] args)
    {
        if (PerformanceCounterCategory.Exists(CategoryName))
            PerformanceCounterCategory.Delete(CategoryName);

        var counterDataCollection = new CounterCreationDataCollection();

        var clearStateCounterData = new CounterCreationData()
        {
            CounterName = CounterName,
            CounterHelp = "The number of times the service state has been cleared since the last performance iteration",
            CounterType = PerformanceCounterType.CounterDelta32
        };
        counterDataCollection.Add(clearStateCounterData);

        PerformanceCounterCategory.Create(CategoryName, "Test Perf Counters", PerformanceCounterCategoryType.SingleInstance, counterDataCollection);

        var counter = new PerformanceCounter(CategoryName, CounterName, false);

        for (int i = 0; i < 5000; i++)
        {
            var sw = Stopwatch.StartNew();
            Thread.Sleep(10300);
            sw.Stop();

            counter.Increment();
        }

        Console.Read();
    }
}
爱的故事 2024-08-15 17:49:49

这不足以创建计数器...根据文档,您需要创建一个PerformanceCounterCategory并创建一个PerformanceCounter实例。查看 MSDN 中的示例: http://msdn.microsoft .com/en-us/library/system.diagnostics.performancecounter.aspx

This is not enough to create the counter... according to the documentation, you need to create a PerformanceCounterCategory and create an instance of PerformanceCounter. Check out the sample in MSDN : http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx

画离情绘悲伤 2024-08-15 17:49:49

创建计数器(使用 CounterCreationData 和 PerformanceCounterCategory 中的 Create),然后创建计数器的实例(使用 PerformanceCounter)后,您需要初始化计数器值以在性能监视器中启动该实例。

另外,请确保您以读写模式创建计数器(通过将 false 传递给 readOnly 参数)。

您可以尝试设置RawValue = RawValue,或RawValue = 0来启动它,看看它是否出现。

Once you create the counter (using the CounterCreationData and the Create in PerformanceCounterCategory), and then create an instance of the counter (using PerformanceCounter), you need to initialize the counter value to get the instance started in Performance Monitor.

Also, make sure you are creating the counter in read-write mode (by passing false to the readOnly argument).

You could try setting RawValue = RawValue, or RawValue = 0 to start it up and see if it appears.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文