Performance Counter Logger

ActivityMonitorIcon 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(
                        "% Processor Time",
                using (
                    var mem = new PerformanceCounter(
                        "Working Set",
                    var step = 1UL;
                    var length = ulong.MaxValue.ToString().Length;
                    var fixTime = new Stopwatch();
                    while (work)
                                step.ToString().PadLeft(length, '0'),
                                string.Format("{0:F2}", cpu.NextValue()),
                                string.Format("{0:F2}", mem.NextValue()));
                            unchecked {
                        catch { }
            { IsBackground = true };
            work = false;

p ;).

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.