The .NET diagnostics CLI toolbox: what dotnet-counters, dotnet-trace, dotnet-dump, dotnet-gcdump and dotnet-monitor each capture, their overhead, and the order you use them in.
The .NET diagnostics tools all talk to the runtime over its diagnostics IPC channel, so they work on Windows, Linux and in containers without a debugger.
I start with counters because they are cheap, then pick the heavier tool the symptom points to. The trade-off is overhead and data sensitivity: a full dump contains every secret in memory.
"All five tools sit on the same plumbing: every .NET process opens a diagnostics IPC channel, a Unix domain socket or a named pipe, and the tools use it to start EventPipe sessions or ask for a dump. That's why they work the same on Windows, Linux and inside a container, and why none of them needs a debugger attached.
I use them in a fixed order. First dotnet-counters, because it's cheap and answers 'what kind of problem is this?'. If CPU is high, I look at the ThreadPool queue and lock contention. If memory grows, I watch GC heap size and Gen 2 collections. If latency climbs with low CPU, I suspect blocking or I/O. Then I pick the specialist tool. dotnet-trace records CPU samples and runtime events into a .nettrace file I open in PerfView, Visual Studio or Speedscope to see hot call stacks. For memory I take two dotnet-gcdump snapshots and compare them, because a gcdump only holds the managed object graph and is much smaller than a full dump. dotnet-dump is the heavy one: the whole process memory, which I analyse with SOS commands like
clrstack,dumpheap -statandgcroot, mainly for hangs, deadlocks and native memory questions.dotnet-monitor is the production version: it runs as a sidecar or tool, exposes the same collections over an authenticated HTTP API, and can collect automatically on triggers like 'CPU above 80% for a minute'. The mistake I see is people taking a full dump first. It pauses the process, can be gigabytes, and holds customer data, so it should be the last tool, not the first."
| Tool | Captures | Overhead | Typical question it answers |
|---|---|---|---|
dotnet-counters |
Live metrics from System.Runtime, Microsoft.AspNetCore.Hosting, System.Net.Http, custom Meters |
Negligible | Is it CPU, GC, ThreadPool or I/O? |
dotnet-trace |
EventPipe events: CPU sampling, GC, exceptions, contention, custom EventSource |
Low to moderate (CPU sampling ~1 ms interval) | Which methods burn CPU or wait? |
dotnet-gcdump |
Managed heap graph (types, counts, sizes, references) | Triggers a full blocking Gen 2 GC | What is filling the heap, and who holds it? |
dotnet-dump |
Full process memory | Pauses the process; file size equals working set | Why is it hung? What is on every thread's stack? |
dotnet-monitor |
All of the above via REST, plus triggers and egress | Same as the underlying collection | How do I collect safely in a container? |
| Symptom | First | Then |
|---|---|---|
| High CPU | counters (cpu-usage, threadpool-queue-length) |
trace with the CPU sampling profile |
| Memory grows | counters (gc-heap-size, gen-2-gc-count) |
two gcdumps, compare |
| Requests hang | counters (ThreadPool thread count rising) | dump, clrstack -all / parallelstacks |
| Latency up, CPU low | counters (monitor-lock-contention-count, ThreadPool queue) |
trace with contention events |
An API's memory climbed from 400 MB to 2.5 GB over a day. dotnet-counters showed the GC heap tracking the working set, so it was managed memory. Two dotnet-gcdump snapshots an hour apart showed 1.2 million extra CacheEntry objects rooted by a static dictionary with no eviction. Adding a size limit flattened memory at 450 MB.
Checks that the candidate knows what each diagnostics tool captures and can pick the right one for a symptom instead of guessing.
Green flags
Red flags
Every .NET Core 3.0+ process creates a diagnostics endpoint: a named pipe on Windows, and a Unix domain socket in $TMPDIR named dotnet-diagnostic-{pid}-...-socket on Linux and macOS. The diagnostics client library (Microsoft.Diagnostics.NETCore.Client) speaks a small binary protocol over it.
dotnet-counters ββ
dotnet-trace βββ€ diagnostics IPC ββββββββββββββββββββββββ
dotnet-gcdump βββΌββββββββββββββββββββββΆβ .NET runtime β
dotnet-dump βββ€ (socket / pipe) β EventPipe, dump β
dotnet-monitor βββ β writer, GC, JIT β
ββββββββββββββββββββββββ
In containers the socket lives in the container's /tmp, so a sidecar must share that volume, or you set DOTNET_DiagnosticPorts to point the runtime at dotnet-monitor's listening socket.
EventPipe is the runtime's cross-platform event tracing system (the equivalent of ETW on Windows). Providers such as Microsoft-Windows-DotNETRuntime (GC, JIT, exceptions, contention) and Microsoft-DotNETCore-SampleProfiler (stack samples) write events into a buffer, which is streamed to the client.
EventCounters and System.Diagnostics.Metrics instruments at an interval (1 s by default). In .NET 8+ the built-in metrics also appear as System.Runtime meters.cpu-sampling (the default), gc-verbose or gc-collect, or explicit --providers.dotnet-gcdump asks the runtime to perform a full, blocking GC and emit events that describe every live object and reference. The result is a graph without object contents, so it is small and contains little sensitive data. dotnet-dump collect writes the whole address space (or a heap-only/mini dump with --type). It shows field values, native memory and every thread's stack, so it can answer anything, but it is large, pauses the process and must be treated as sensitive data under the Privacy Act's security obligations.
dotnet-monitor (with a .NET 10 release) runs as a global tool or the mcr.microsoft.com/dotnet/monitor container image. It exposes /processes, /trace, /gcdump, /dump, /metrics (Prometheus format) and supports collection rules: trigger (CPU, GC heap size, request duration, exception) β action (collect trace or dump) β egress (Azure Blob Storage). It needs authentication (API key or Entra ID) because a dump endpoint is effectively a data-exfiltration endpoint.
| Collection | Cost |
|---|---|
| Counters | Under 1% CPU |
| CPU sampling trace | A few % CPU while running; file grows ~MB per second |
| gcdump | A full blocking GC: seconds on a multi-GB heap |
| Full dump | Process frozen while written; disk equal to memory |
dotnet tool install -g dotnet-counters dotnet-trace dotnet-dump dotnet-gcdump.k6 or bombardier, and watch dotnet-counters monitor -n MyApi --counters System.Runtime,Microsoft.AspNetCore.Hosting.dotnet-trace collect -n MyApi --duration 00:00:30 and open the .nettrace in PerfView or convert it with --format speedscope./tmp volume and call /gcdump over HTTP.Typical command sequence for a misbehaving service, from cheapest to heaviest.
# 1. Find the process
dotnet-counters ps
# 2. Cheap live view: what kind of problem is it?
dotnet-counters monitor -n Orders.Api \
--counters System.Runtime,Microsoft.AspNetCore.Hosting,System.Net.Http
# 3a. CPU or latency: record 30 s of CPU samples and runtime events
dotnet-trace collect -n Orders.Api --profile cpu-sampling --duration 00:00:30 \
--format speedscope
# 3b. Memory growth: two heap snapshots a few minutes apart, then compare
dotnet-gcdump collect -n Orders.Api -o before.gcdump
dotnet-gcdump collect -n Orders.Api -o after.gcdump
dotnet-gcdump report after.gcdump | head -30
# 3c. Hang or deadlock: full dump, then SOS
dotnet-dump collect -n Orders.Api --type Full -o hang.dmp
dotnet-dump analyze hang.dmp
# > clrthreads
# > parallelstacks
# > dumpheap -stat
A custom metric so dotnet-counters shows business signals next to runtime ones:
using System.Diagnostics.Metrics;
namespace Orders.Api.Diagnostics;
public sealed class OrderMetrics
{
// The meter name is what you pass to dotnet-counters --counters.
public const string MeterName = "Orders.Api";
private readonly Counter<long> _ordersPlaced;
private readonly Histogram<double> _pricingMs;
public OrderMetrics(IMeterFactory meterFactory)
{
var meter = meterFactory.Create(MeterName);
_ordersPlaced = meter.CreateCounter<long>("orders.placed");
_pricingMs = meter.CreateHistogram<double>("orders.pricing.duration", unit: "ms");
}
public void OrderPlaced() => _ordersPlaced.Add(1);
public void PricingTook(TimeSpan elapsed) => _pricingMs.Record(elapsed.TotalMilliseconds);
}
// Program.cs
// builder.Services.AddSingleton<OrderMetrics>();
// then: dotnet-counters monitor -n Orders.Api --counters Orders.Api
/tmp with a diagnostics sidecar in Kubernetes. Why it is wrong: The tools can't find the diagnostics socket. Instead: Mount a shared emptyDir or set DOTNET_DiagnosticPorts.Knowing the tools is junior; the senior part is choosing the cheapest tool that can answer the question and treating diagnostic artefacts as production data. These CLI tools are for the investigation, not the detection: continuous monitoring (the same runtime metrics exported through OpenTelemetry to Azure Monitor or Prometheus, with alerts on p95 latency, GC heap and ThreadPool queue length) should tell you something is wrong before users do, and give you the shape of the problem before anyone opens a terminal. I also want dotnet-monitor or an equivalent pre-installed so a trace in production is one HTTP call rather than a change request.
Counters first is a default, not a rule: when the symptom is a short burst (a 200 ms stall every few minutes), I start a dotnet-trace or a dotnet-monitor trigger straight away, because 1-second averages will hide it.
| Tool | When it fits | Cost | When not to |
|---|---|---|---|
| dotnet-counters | Every investigation, first | None to speak of | Millisecond-scale transient events |
| dotnet-trace | CPU, latency, contention, transient events | Some CPU, large files | Memory leaks (use gcdump) |
| dotnet-gcdump | Managed memory growth | Blocking full GC | Native memory, hangs |
| dotnet-dump | Hangs, deadlocks, native memory, crashes | Process pause, big sensitive file | Routine CPU work |
| dotnet-monitor | Containers, automated triggers | Running a sidecar, securing it | A dev laptop |
I look at dotnet-counters (System.Runtime) next to request and dependency telemetry:
dotnet-trace.If CPU is low and the ThreadPool queue keeps growing, it's blocking (sync-over-async), not I/O capacity. The trade-off: one counter never proves it; I confirm with a profile or trace.
When I can't attach a debugger, I take a dump: a snapshot of the process's memory, threads and stacks that I analyse offline. For a hang or a leak, dotnet-dump collect -p <pid> does it in seconds. For crashes, I enable automatic crash dumps with the DOTNET_DbgEnableMiniDump environment variables. On App Service I use Diagnose and solve problems, and in Kubernetes dotnet-monitor. Then I analyse with dotnet-dump analyze: clrstack -all and syncblk for hangs, dumpheap -stat and gcroot for leaks, pe for exceptions. Or I open the dump in Visual Studio and use the Threads and Parallel Stacks windows. Dumps contain customer data, so they're handled as sensitive.
First I measure: Application Insights tells me which endpoint is slow and at what percentile, and whether time goes into dependencies like SQL or HTTP. Then I reproduce it on a Release build and run the Visual Studio Performance Profiler (Alt+F2): CPU Usage for hot paths, Database for query count and duration, .NET Object Allocation Tracking for GC pressure, and .NET Async for slow awaits. In an environment where I can't use Visual Studio, dotnet-counters shows live metrics like thread pool queue length and GC, and dotnet-trace captures CPU samples. I fix the biggest cause, often an N+1 query, re-measure against the baseline, and add a load test so it doesn't regress.
Last updated 23 Nov 2026.