System.Diagnostics.PerformanceCounter 11.0.0-rc.1.26425.128

About

This package provides types that allow applications to interact with the Windows performance counters.

Windows allows you to examine how programs you run affect your computer's performance, both in real time and by collecting log data for later analysis. You can do this via the Windows Performance Monitor tool, which uses performance counters, among other features.

Windows performance counters provide a high-level abstraction layer that provides a consistent interface for collecting various kinds of system data such as CPU, memory, and disk usage. They can be included in the operating system or can be part of individual applications. Windows Performance Monitor requests the current value of performance counters at specifiedtime intervals.

System administrators often use performance counters to monitor systems for performance or behavior problems. Software developers often use performance counters to examine the resource usage of their programs.

Key Features

  • Can be used to read existing predefined or custom counters.
  • Can be used for publishing (writing) data to custom counters.
  • Can collect performance counters from the local machine or from a remote machine.

How to Use

using System;
using System.Collections.Generic;
using System.Diagnostics;

public class App
{
    public static void Main()
    {
        List<CounterSample> samples = [];

        // If the category does not exist, create the category and exit.
        // Performance counters should not be created and immediately used.
        // There is a latency time to enable the counters, they should be created
        // prior to executing the application that uses the counters.
        // Execute this sample a second time to use the category.
        if (SetupCategory())
        {
            return;
        }

        CollectSamples(samples);
        CalculateResults(samples);
    }

    private static bool SetupCategory()
    {
        if (PerformanceCounterCategory.Exists("AverageCounter64SampleCategory"))
        {
            Console.WriteLine("Category exists - AverageCounter64SampleCategory");
            return false;
        }

        CounterCreationDataCollection counterDataCollection = [];

        // Add the counter.
        CounterCreationData averageCount64 = new()
        {
            CounterType = PerformanceCounterType.AverageCount64,
            CounterName = "AverageCounter64Sample"
        };
        counterDataCollection.Add(averageCount64);

        // Add the base counter.
        CounterCreationData averageCount64Base = new()
        {
            CounterType = PerformanceCounterType.AverageBase,
            CounterName = "AverageCounter64SampleBase"
        };
        counterDataCollection.Add(averageCount64Base);

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

        return true;
    }

    private static void CollectSamples(List<CounterSample> samples)
    {
        // Create the counters

        PerformanceCounter avgCounter64Sample = new PerformanceCounter("AverageCounter64SampleCategory",
            "AverageCounter64Sample",
            false)
        {
            RawValue = 0
        };

        PerformanceCounter avgCounter64SampleBase = new PerformanceCounter("AverageCounter64SampleCategory",
            "AverageCounter64SampleBase",
            false)
        {
            RawValue = 0
        };

        Random r = new(DateTime.Now.Millisecond);

        for (int j = 0; j < 100; j++)
        {
            int value = r.Next(1, 10);
            Console.Write(j + " = " + value);

            avgCounter64Sample.IncrementBy(value);

            avgCounter64SampleBase.Increment();

            if ((j % 10) == 9)
            {
                OutputSample(avgCounter64Sample.NextSample());
                samples.Add(avgCounter64Sample.NextSample());
            }
            else
            {
                Console.WriteLine();
            }

            System.Threading.Thread.Sleep(50);
        }
    }

    private static void CalculateResults(List<CounterSample> samples)
    {
        for (int i = 0; i < (samples.Count - 1); i++)
        {
            // Output the sample.
            OutputSample(samples[i]);
            OutputSample(samples[i + 1]);

            // Use .NET to calculate the counter value.
            Console.WriteLine($".NET computed counter value = {CounterSampleCalculator.ComputeCounterValue(samples[i], samples[i + 1])}");

            // Calculate the counter value manually.
            Console.WriteLine($"My computed counter value = {MyComputeCounterValue(samples[i], samples[i + 1])}");
        }
    }

    //    Description - This counter type shows how many items are processed, on average,
    //        during an operation. Counters of this type display a ratio of the items
    //        processed (such as bytes sent) to the number of operations completed. The
    //        ratio is calculated by comparing the number of items processed during the
    //        last interval to the number of operations completed during the last interval.
    // Generic type - Average
    //      Formula - (N1 - N0) / (D1 - D0), where the numerator (N) represents the number
    //        of items processed during the last sample interval and the denominator (D)
    //        represents the number of operations completed during the last two sample
    //        intervals.
    //    Average (Nx - N0) / (Dx - D0)
    //    Example PhysicalDisk\ Avg. Disk Bytes/Transfer
    private static float MyComputeCounterValue(CounterSample s0, CounterSample s1)
    {
        float numerator = (float)s1.RawValue - s0.RawValue;
        float denomenator = (float)s1.BaseValue - s0.BaseValue;
        float counterValue = numerator / denomenator;
        return counterValue;
    }

    private static void OutputSample(CounterSample s)
    {
        Console.WriteLine("\r\n+++++++++++");
        Console.WriteLine("Sample values - \r\n");
        Console.WriteLine($"   BaseValue        = {s.BaseValue}");
        Console.WriteLine($"   CounterFrequency = {s.CounterFrequency}");
        Console.WriteLine($"   CounterTimeStamp = {s.CounterTimeStamp}");
        Console.WriteLine($"   CounterType      = {s.CounterType}");
        Console.WriteLine($"   RawValue         = {s.RawValue}");
        Console.WriteLine($"   SystemFrequency  = {s.SystemFrequency}");
        Console.WriteLine($"   TimeStamp        = {s.TimeStamp}");
        Console.WriteLine($"   TimeStamp100nSec = {s.TimeStamp100nSec}");
        Console.WriteLine("++++++++++++++++++++++");
    }
}

Notes:

  • This assembly is only supported on Windows operating systems.
  • Only the administrator of the computer or users in the Performance Logs User Group can log counter data. Users in the Administrator group can log counter data only if the tool they use to log counter data is started from a Command Prompt window that is opened with Run as administrator.... Any users in interactive logon sessions can view counter data. However, users in non-interactive logon sessions must be in the Performance Monitoring Users group to view counter data.

Main Types

The main types provided by this library are:

Under the System.Diagnostics namespace, the main types are:

Under the System.Diagnostics.PerformanceData namespace, the main types are:

Additional Documentation

Feedback & Contributing

System.Diagnostics.PerformanceCounter is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.

Showing the top 20 packages that depend on System.Diagnostics.PerformanceCounter.

Packages Downloads
Microsoft.ApplicationInsights.PerfCounterCollector
Application Insights Performance Counters Collector allows you to send data collected by Performance Counters to Application Insights. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156
158
Microsoft.ApplicationInsights.PerfCounterCollector
Application Insights Performance Counters Collector allows you to send data collected by Performance Counters to Application Insights. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156
160
Microsoft.ApplicationInsights.PerfCounterCollector
Application Insights Performance Counters Collector allows you to send data collected by Performance Counters to Application Insights. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156
166
Microsoft.ApplicationInsights.PerfCounterCollector
Application Insights Performance Counters Collector allows you to send data collected by Performance Counters to Application Insights. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156
167
Microsoft.ApplicationInsights.PerfCounterCollector
Application Insights Performance Counters Collector allows you to send data collected by Performance Counters to Application Insights. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156
170
Microsoft.ApplicationInsights.PerfCounterCollector
Application Insights Performance Counters Collector allows you to send data collected by Performance Counters to Application Insights. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156
172
StackExchange.Redis
High performance Redis client, incorporating both synchronous and asynchronous usage.
156
StackExchange.Redis
High performance Redis client, incorporating both synchronous and asynchronous usage.
157
StackExchange.Redis
High performance Redis client, incorporating both synchronous and asynchronous usage.
158
StackExchange.Redis
High performance Redis client, incorporating both synchronous and asynchronous usage.
159
StackExchange.Redis
High performance Redis client, incorporating both synchronous and asynchronous usage.
160
StackExchange.Redis
High performance Redis client, incorporating both synchronous and asynchronous usage.
161
StackExchange.Redis
High performance Redis client, incorporating both synchronous and asynchronous usage.
163
StackExchange.Redis
High performance Redis client, incorporating both synchronous and asynchronous usage.
174

https://go.microsoft.com/fwlink/?LinkID=799421

.NET 10.0

.NET 11.0

.NET Framework 4.6.2

  • No dependencies.

.NET Standard 2.0

  • No dependencies.

Version Downloads Last updated
11.0.0-rc.1.26425.128 2 09/09/2026
11.0.0-preview.7.26381.103 16 08/15/2026
11.0.0-preview.6.26359.118 22 07/15/2026
11.0.0-preview.5.26302.115 28 06/09/2026
11.0.0-preview.4.26230.115 32 05/13/2026
11.0.0-preview.3.26207.106 38 04/14/2026
11.0.0-preview.2.26159.112 45 03/11/2026
11.0.0-preview.1.26104.118 37 02/11/2026
10.0.12 3 09/09/2026
10.0.11 15 08/15/2026
10.0.10 16 07/16/2026
10.0.9 21 06/09/2026
10.0.8 30 05/13/2026
10.0.7 35 04/21/2026
10.0.6 39 04/14/2026
10.0.5 49 03/13/2026
10.0.4 47 03/11/2026
10.0.3 58 02/11/2026
10.0.2 46 01/13/2026
10.0.1 77 12/10/2025
10.0.0 90 11/12/2025
10.0.0-rc.2.25502.107 89 10/14/2025
10.0.0-rc.1.25451.107 105 09/09/2025
10.0.0-preview.7.25380.108 111 08/14/2025
10.0.0-preview.6.25358.103 100 07/17/2025
10.0.0-preview.5.25277.114 116 06/08/2025
10.0.0-preview.4.25258.110 98 05/15/2025
10.0.0-preview.3.25171.5 103 04/13/2025
10.0.0-preview.2.25163.2 120 04/01/2025
10.0.0-preview.1.25080.5 133 04/01/2025
9.0.20 2 09/09/2026
9.0.19 17 08/15/2026
9.0.18 15 07/16/2026
9.0.17 24 06/09/2026
9.0.16 26 05/13/2026
9.0.15 34 04/14/2026
9.0.14 31 03/11/2026
9.0.13 41 02/11/2026
9.0.12 44 01/14/2026
9.0.11 81 11/12/2025
9.0.10 92 10/15/2025
9.0.9 120 09/09/2025
9.0.8 90 08/05/2025
9.0.7 127 07/09/2025
9.0.6 111 06/14/2025
9.0.5 105 05/16/2025
9.0.4 150 04/09/2025
9.0.3 117 03/30/2025
9.0.2 132 03/30/2025
9.0.1 149 03/30/2025
9.0.0 143 11/14/2024
9.0.0-rc.2.24473.5 122 10/21/2024
9.0.0-rc.1.24431.7 136 09/20/2024
9.0.0-preview.7.24405.7 125 08/20/2024
9.0.0-preview.6.24327.7 165 07/21/2024
9.0.0-preview.5.24306.7 143 07/21/2024
9.0.0-preview.4.24266.19 137 07/21/2024
9.0.0-preview.3.24172.9 174 07/21/2024
9.0.0-preview.2.24128.5 140 07/21/2024
9.0.0-preview.1.24080.9 152 07/21/2024
8.0.1 155 10/23/2024
8.0.0 159 07/21/2024
8.0.0-rc.2.23479.6 128 07/21/2024
8.0.0-rc.1.23419.4 143 07/21/2024
8.0.0-preview.7.23375.6 143 07/21/2024
8.0.0-preview.6.23329.7 140 07/21/2024
8.0.0-preview.5.23280.8 141 07/21/2024
8.0.0-preview.4.23259.5 154 07/21/2024
8.0.0-preview.3.23174.8 138 07/21/2024
8.0.0-preview.2.23128.3 149 07/21/2024
8.0.0-preview.1.23110.8 156 07/21/2024
7.0.0 136 07/21/2024
7.0.0-rc.2.22472.3 144 07/21/2024
7.0.0-rc.1.22426.10 138 07/21/2024
7.0.0-preview.7.22375.6 140 07/21/2024
7.0.0-preview.6.22324.4 135 07/21/2024
7.0.0-preview.5.22301.12 136 07/21/2024
7.0.0-preview.4.22229.4 135 07/21/2024
7.0.0-preview.3.22175.4 143 07/21/2024
7.0.0-preview.2.22152.2 122 07/21/2024
7.0.0-preview.1.22076.8 131 07/21/2024
6.0.2 112 11/14/2024
6.0.1 143 07/21/2024
6.0.0 134 07/21/2024
6.0.0-rc.2.21480.5 121 07/21/2024
6.0.0-rc.1.21451.13 150 07/21/2024
6.0.0-preview.7.21377.19 128 07/21/2024
6.0.0-preview.6.21352.12 142 07/21/2024
6.0.0-preview.5.21301.5 137 07/21/2024
6.0.0-preview.4.21253.7 158 07/21/2024
6.0.0-preview.3.21201.4 153 07/21/2024
6.0.0-preview.2.21154.6 132 07/21/2024
6.0.0-preview.1.21102.12 143 07/21/2024
5.0.1 130 07/21/2024
5.0.0 134 07/21/2024
5.0.0-rc.2.20475.5 149 07/21/2024
5.0.0-rc.1.20451.14 129 07/21/2024
5.0.0-preview.8.20407.11 123 07/21/2024
5.0.0-preview.7.20364.11 137 07/21/2024
5.0.0-preview.6.20305.6 147 07/21/2024
5.0.0-preview.5.20278.1 132 07/21/2024
5.0.0-preview.4.20251.6 145 07/21/2024
5.0.0-preview.3.20214.6 136 07/21/2024
5.0.0-preview.2.20160.6 151 07/21/2024
5.0.0-preview.1.20120.5 155 07/21/2024
4.7.0 136 07/21/2024
4.7.0-preview3.19551.4 132 07/21/2024
4.7.0-preview2.19523.17 134 07/21/2024
4.7.0-preview1.19504.10 143 07/21/2024
4.6.0 153 07/21/2024
4.6.0-rc1.19456.4 136 07/21/2024
4.6.0-preview9.19421.4 142 07/21/2024
4.6.0-preview9.19416.11 128 07/21/2024
4.6.0-preview8.19405.3 131 07/21/2024
4.6.0-preview7.19362.9 151 07/21/2024
4.6.0-preview6.19303.8 136 07/21/2024
4.6.0-preview6.19264.9 132 07/21/2024
4.6.0-preview5.19224.8 147 07/21/2024
4.6.0-preview4.19212.13 146 07/21/2024
4.6.0-preview3.19128.7 131 07/21/2024
4.6.0-preview.19073.11 137 07/21/2024
4.6.0-preview.18571.3 134 07/21/2024
4.5.0 120 07/21/2024
4.5.0-rc1 144 07/21/2024
4.5.0-preview2-26406-04 137 07/21/2024
4.5.0-preview1-26216-02 152 07/21/2024
4.5.0-preview1-25914-04 127 07/21/2024