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 process not working. So I wrote Performance Counter Logger to solve that issue. it is very small almost trivial tool which has 60 lines of C# code. Tool was created by me today to help get performance counters on Windows of CPU and Memory usage by 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 values of CPU and Memory usage every second and produces CSV log file line by line. Rest of the work is open it in Excel and create charts for example. I like this idea because it is very little resource consuming logger for monitoring. The one more thing is that it can be put on any Windows machine and collect data that could be get latter to analyze. 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.