Hi, today I would like to share with you idea of the Performance Counter Logger. This tool is for logging performance counters values, and it solves issues with built-in PerfMon in Windows that collect data only for some time window and cannot auto-pause when the process not working. So I wrote Performance Counter Logger to solve that issue. It is a minimal, almost trivial tool with 60 lines of C# code. The tool was created by me today to help get performance counters on Windows of CPU and Memory usage by the process I wanted to monitor. Usage of this idea is very simple and it works in following way in command line on Windows.
PerformanceCounterLogger.exe PROCESS_NAME > log.csv
And when you want to stop logging just press any key on keyboard. Performance Counter Logger collects CPU and Memory usage values every second and produces CSV log file line by line. The rest of the work is to open in Excel and create charts, for example. I like this idea because it is a minimal resource-consuming logger for monitoring. One more thing is that it can be put on any Windows machine and collect data that could be analyzed later. Of course, you can add more performance counters if you like. This is more the idea, not the finished solution. Enjoy!
namespace PerformanceCounterLogger { using System; using System.Diagnostics; using System.Globalization; using System.Threading; class PerformanceCounterLogger { private static volatile bool work = false; static void Main(string[] args) { if (args.Length == 0 || string.IsNullOrEmpty(args[0])) return; var processName = args[0]; work = true; var thread = new Thread(() => { using ( var cpu = new PerformanceCounter( "Process", "% Processor Time", processName)) using ( var mem = new PerformanceCounter( "Process", "Working Set", processName)) { var step = 1UL; var length = ulong.MaxValue.ToString().Length; var fixTime = new Stopwatch(); while (work) { fixTime.Start(); try { Console.WriteLine( "{1}{0}{2}{0}{3}", CultureInfo.CurrentCulture.TextInfo.ListSeparator, step.ToString().PadLeft(length, '0'), string.Format("{0:F2}", cpu.NextValue()), string.Format("{0:F2}", mem.NextValue())); unchecked { ++step; } } catch { } Thread.Sleep(1000-(int)fixTime.ElapsedMilliseconds); fixTime.Reset(); } } }) { IsBackground = true }; thread.Start(); Console.ReadKey(true); work = false; thread.Join(); } } }
p ;).