你能打开perfmon.exe,清除所有当前计数并添加自定义应用程序计数器吗?

时间:2015-07-02 10:02:32

标签: c# .net perfmon

您可以打开perfmon.exe,清除所有当前计数并从C#添加自定义应用程序计数器吗?

考虑一下perfmon API,但我找不到它。

2 个答案:

答案 0 :(得分:4)

性能计数器很不适合跟踪应用程序级指标。

在Linux / Unix世界中,有一个出色的Graphite和StatsD组合,我们已将它移植到.NET:Statsify

它允许您从应用程序中收集各种指标:数据库查询的数量,调用Web服务所需的时间,跟踪活动连接的数量等等 - 所有这些都使用简单的API,如

Stats.Increment("db.query.count");

答案 1 :(得分:0)

您可以使用 PerformanceCounter 类,即int System.System.Diagnostics 命名空间。

要添加您自己的类别和计数器,请使用以下代码:

if (!PerformanceCounterCategory.Exists("AverageCounter64SampleCategory"))
        {

            CounterCreationDataCollection CCDC = new CounterCreationDataCollection();

            // Add the counter.
            CounterCreationData averageCount64 = new CounterCreationData();
            averageCount64.CounterType = PerformanceCounterType.AverageCount64;
            averageCount64.CounterName = "AverageCounter64Sample";
            CCDC.Add(averageCount64);

            // Add the base counter.
            CounterCreationData averageCount64Base = new CounterCreationData();
            averageCount64Base.CounterType = PerformanceCounterType.AverageBase;
            averageCount64Base.CounterName = "AverageCounter64SampleBase";
            CCDC.Add(averageCount64Base);

            // Create the category.
            PerformanceCounterCategory.Create("AverageCounter64SampleCategory",
                "Demonstrates usage of the AverageCounter64 performance counter type.",
                CCDC);

        }

要清除计数器,您可以将 RawValue 重置为0,如下所示:

var pc = new PerformanceCounter("AverageCounter64SampleCategory",
            "AverageCounter64Sample",
            false);

        pc.RawValue = 0;

上面的这些示例代码来自此链接:system.diagnostics.performancecounter

希望它有所帮助。