ContextSnapshotBuilder Class

A builder for creating diagnostic snapshots of the current execution context. Use this to gather system, runtime, and process information for troubleshooting.

public sealed class ContextSnapshotBuilder

Example

Capture comprehensive system diagnostics:

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

// Serialize for logging
var json = JsonSerializer.Serialize(snapshot, new JsonSerializerOptions(JsonSerializerDefaults.Strict) { WriteIndented = true });
Assert.StartsWith("{", json);

Constructors

NameDescription
ContextSnapshotBuilder()

ContextSnapshotBuilder()

ContextSnapshotBuilder.cs
public ContextSnapshotBuilder()

No documentation is available.

Methods

NameDescription
AddAppContextData()Adds all AppContext data and switches using reflection to access internal stores.
AddAssemblyLoadContexts()Adds information about loaded assembly load contexts and their assemblies.
AddCommonAppContext()Adds commonly used AppContext switches and data values related to runtime configuration.
AddConsole()Adds console information including encoding, buffer size, and colors.
AddCpu()Adds CPU information including processor name, core count, and hardware intrinsics support.
AddCulture()Adds culture and globalization settings.
AddCurrentDirectory()Adds the current working directory path.
AddCurrentProcess()Adds information about the current process including command line, memory usage, and processor time.
AddDefault()Adds all available diagnostic information including environment variables, GC, drives, processes, OS details, and more.
AddDotnetRuntime()Adds .NET runtime information including runtime identifier and framework description.
AddDrives()Adds information about all available drives including free space and drive type.
AddEnvironmentVariables(EnvironmentVariableTarget)Adds environment variables for the specified target scope, redacting values whose name looks like a secret.
AddEnvironmentVariables(EnvironmentVariableTarget, Func<string, bool>)Adds environment variables for the specified target scope.
AddGarbageCollector()Adds garbage collector information including generation count and memory usage.
AddHypervisor()Adds hypervisor information when running in a virtualized environment (Windows only).
AddLocalTimeZone()Adds local time zone information including UTC offset and daylight saving time support.
AddMachineName()Adds the machine name.
AddNewLine()Adds the newline string for the current platform.
AddOperatingSystem()Adds operating system information including platform, version, and architecture.
AddPowerManagement()Adds the active power management scheme (Windows only).
AddProcessorCount()Adds the number of logical processors available to the current process.
AddRuntimeFeatures()Adds supported .NET runtime features.
AddSecurityProviders()Adds security provider information including antivirus, firewall, and anti-spyware status (Windows only).
AddSpecialFolderPaths()Adds paths to special system folders.
AddThreadPool()Adds thread pool configuration including worker thread and completion port thread counts.
AddUser()Adds current user information including username and domain.
AddValue(string, object?)Adds a custom key-value pair to the snapshot.
BuildSnapshot()Builds and returns the snapshot as a sorted dictionary of key-value pairs.
IsSecretEnvironmentVariableName(string)Determines whether the value of an environment variable is replaced by ContextSnapshotBuilder.RedactedValue. Defaults to a name-based match on fragments such as TOKEN, SECRET and PASSWORD.

AddAppContextData()

ContextSnapshotBuilder.cs

Adds all AppContext data and switches using reflection to access internal stores.

public ContextSnapshotBuilder AddAppContextData()

AddAssemblyLoadContexts()

ContextSnapshotBuilder.cs

Adds information about loaded assembly load contexts and their assemblies.

public ContextSnapshotBuilder AddAssemblyLoadContexts()

AddCommonAppContext()

ContextSnapshotBuilder.cs

Adds commonly used AppContext switches and data values related to runtime configuration.

public ContextSnapshotBuilder AddCommonAppContext()

Adds console information including encoding, buffer size, and colors.

public ContextSnapshotBuilder AddConsole()

Adds CPU information including processor name, core count, and hardware intrinsics support.

public ContextSnapshotBuilder AddCpu()

Adds culture and globalization settings.

public ContextSnapshotBuilder AddCulture()

AddCurrentDirectory()

ContextSnapshotBuilder.cs

Adds the current working directory path.

public ContextSnapshotBuilder AddCurrentDirectory()

AddCurrentProcess()

ContextSnapshotBuilder.cs

Adds information about the current process including command line, memory usage, and processor time.

public ContextSnapshotBuilder AddCurrentProcess()

Adds all available diagnostic information including environment variables, GC, drives, processes, OS details, and more.

public ContextSnapshotBuilder AddDefault()

AddDotnetRuntime()

ContextSnapshotBuilder.cs

Adds .NET runtime information including runtime identifier and framework description.

public ContextSnapshotBuilder AddDotnetRuntime()

Adds information about all available drives including free space and drive type.

public ContextSnapshotBuilder AddDrives()

AddEnvironmentVariables overloads

AddEnvironmentVariables(EnvironmentVariableTarget)

ContextSnapshotBuilder.cs

Adds environment variables for the specified target scope, redacting values whose name looks like a secret.

AddEnvironmentVariables(EnvironmentVariableTarget, Func<string, bool>)

ContextSnapshotBuilder.cs

Adds environment variables for the specified target scope.

public ContextSnapshotBuilder AddEnvironmentVariables(EnvironmentVariableTarget target, Func<string, bool> shouldRedact)
Parameters
target EnvironmentVariableTarget
The scope to read the variables from.
shouldRedact Func<string, bool>
Called with each variable name. When it returns true the value is replaced by ContextSnapshotBuilder.RedactedValue. Pass _ => false to capture every value verbatim.
Returns

ContextSnapshotBuilder

AddGarbageCollector()

ContextSnapshotBuilder.cs

Adds garbage collector information including generation count and memory usage.

public ContextSnapshotBuilder AddGarbageCollector()

Adds hypervisor information when running in a virtualized environment (Windows only).

public ContextSnapshotBuilder AddHypervisor()

AddLocalTimeZone()

ContextSnapshotBuilder.cs

Adds local time zone information including UTC offset and daylight saving time support.

public ContextSnapshotBuilder AddLocalTimeZone()

Adds the machine name.

public ContextSnapshotBuilder AddMachineName()

Adds the newline string for the current platform.

public ContextSnapshotBuilder AddNewLine()

AddOperatingSystem()

ContextSnapshotBuilder.cs

Adds operating system information including platform, version, and architecture.

public ContextSnapshotBuilder AddOperatingSystem()

AddPowerManagement()

ContextSnapshotBuilder.cs

Adds the active power management scheme (Windows only).

public ContextSnapshotBuilder AddPowerManagement()

AddProcessorCount()

ContextSnapshotBuilder.cs

Adds the number of logical processors available to the current process.

public ContextSnapshotBuilder AddProcessorCount()

AddRuntimeFeatures()

ContextSnapshotBuilder.cs

Adds supported .NET runtime features.

public ContextSnapshotBuilder AddRuntimeFeatures()

AddSecurityProviders()

ContextSnapshotBuilder.cs

Adds security provider information including antivirus, firewall, and anti-spyware status (Windows only).

public ContextSnapshotBuilder AddSecurityProviders()

AddSpecialFolderPaths()

ContextSnapshotBuilder.cs

Adds paths to special system folders.

public ContextSnapshotBuilder AddSpecialFolderPaths()

Adds thread pool configuration including worker thread and completion port thread counts.

public ContextSnapshotBuilder AddThreadPool()

Adds current user information including username and domain.

public ContextSnapshotBuilder AddUser()

AddValue(string, object?)

ContextSnapshotBuilder.cs

Adds a custom key-value pair to the snapshot.

public ContextSnapshotBuilder AddValue(string key, object? value)

Parameters

key string
value object?

Returns

ContextSnapshotBuilder

Builds and returns the snapshot as a sorted dictionary of key-value pairs.

public IReadOnlyDictionary<string, object?> BuildSnapshot()

IsSecretEnvironmentVariableName(string)

ContextSnapshotBuilder.cs

Determines whether the value of an environment variable is replaced by ContextSnapshotBuilder.RedactedValue. Defaults to a name-based match on fragments such as TOKEN, SECRET and PASSWORD.

public static bool IsSecretEnvironmentVariableName(string name)

Parameters

name string

Returns

bool

Remarks

Name-based redaction is best-effort. It cannot recognise a secret stored under an unremarkable name, so it reduces accidental disclosure rather than guaranteeing a snapshot is safe to publish.

Constants

NameDescription
RedactedValueThe value substituted for environment variables whose name looks like it holds a secret.

RedactedValue

The value substituted for environment variables whose name looks like it holds a secret.

public const string RedactedValue = "***"