Last active
January 28, 2022 21:15
-
-
Save nikvoronin/643b9dc775c648a0397fdf853f70e7b0 to your computer and use it in GitHub Desktop.
Prints all windows performance counters with categories, instances and counters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NuGet package `System.Diagnostics.PerformanceCounter` have to be installed | |
// .NET 6 | |
#nullable disable | |
using System.Globalization; | |
using System.Diagnostics; | |
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; | |
using var writer = new StreamWriter( Console.OpenStandardOutput() ); | |
var cats = PerformanceCounterCategory.GetCategories( Environment.MachineName ); | |
foreach ( var cat in cats ) { | |
writer.WriteLine( $"CATEGORY: {cat.CategoryName}" ); | |
var instances = Array.Empty<string>(); | |
try { | |
instances = cat.GetInstanceNames(); | |
var hasNotInstance = instances.Length == 0; | |
if ( hasNotInstance ) | |
PrintCounters( cat ); | |
else { | |
foreach ( var instanceName in instances ) | |
PrintCounters( cat, instanceName ); | |
} | |
} | |
catch { } | |
void PrintCounters( PerformanceCounterCategory cat, string instanceName = "" ) { | |
if ( !string.IsNullOrEmpty( instanceName ) ) | |
writer.Write( $"\tINSTANCE: {instanceName}\n\t" ); | |
writer.WriteLine( "\tCOUNTERS:" ); | |
var counters = string.IsNullOrEmpty( instanceName ) | |
? cat.GetCounters() | |
: cat.GetCounters( instanceName ); | |
foreach ( var counter in counters ) | |
writer.WriteLine( $"\t\t{counter.CounterName}" ); | |
} | |
} | |
writer.WriteLine( "END." ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment