Getting started

edit

Install the packages

Add the packages to a project:

dotnet package add Meziantou.Framework.Diagnostics.ContextSnapshot
dotnet package add Meziantou.Framework.Diagnostics.MemoryDump

Capture the execution context

ContextSnapshotBuilder collects the information, and BuildSnapshot returns it as a dictionary that you can serialize with System.Text.Json:

var snapshot = new ContextSnapshotBuilder().AddDefault().BuildSnapshot();

// You can serialize the context
var json = JsonSerializer.Serialize(snapshot, new JsonSerializerOptions
{
    RespectNullableAnnotations = true,
    RespectRequiredConstructorParameters = true,
    WriteIndented = true,
    Converters =
    {
        new JsonStringEnumConverter(),
    },
});
  • AddDefault adds everything the package knows how to capture. You can also pick the parts you need: see Capturing a snapshot.
  • JsonStringEnumConverter writes enumeration values, such as the architecture or the platform, as names instead of numbers.
  • The examples need using Meziantou.Framework.Diagnostics.ContextSnapshot;, using System.Text.Json; and using System.Text.Json.Serialization;.

Write a memory dump

MemoryDump.Write writes a dump of the current process to a file:

// Full dump (default)
MemoryDump.Write("app.dmp");

// Smaller dump containing the managed heap
await MemoryDump.WriteAsync("app.dmp", MemoryDumpType.WithHeap, cancellationToken);
  • The default dump is a full dump, as large as the memory of the process. MemoryDumpType.WithHeap is smaller and enough to inspect the managed heap. See Dump types.
  • The example needs using Meziantou.Framework.Diagnostics;.
  • On Linux and macOS, the operating system can prevent the process from being dumped. On macOS in particular, an application started with its own executable (./app) cannot be dumped unless you re-sign it. See Platform requirements.

Next steps