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 ContextSnapshotBuilderExample
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
| Name | Description |
|---|---|
| ContextSnapshotBuilder() |
ContextSnapshotBuilder()
ContextSnapshotBuilder.cspublic ContextSnapshotBuilder()No documentation is available.
Methods
| Name | Description |
|---|---|
| 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.csAdds all AppContext data and switches using reflection to access internal stores.
public ContextSnapshotBuilder AddAppContextData()Returns
AddAssemblyLoadContexts()
ContextSnapshotBuilder.csAdds information about loaded assembly load contexts and their assemblies.
public ContextSnapshotBuilder AddAssemblyLoadContexts()Returns
AddCommonAppContext()
ContextSnapshotBuilder.csAdds commonly used AppContext switches and data values related to runtime configuration.
public ContextSnapshotBuilder AddCommonAppContext()Returns
AddConsole()
ContextSnapshotBuilder.csAdds console information including encoding, buffer size, and colors.
public ContextSnapshotBuilder AddConsole()Returns
AddCpu()
ContextSnapshotBuilder.csAdds CPU information including processor name, core count, and hardware intrinsics support.
public ContextSnapshotBuilder AddCpu()Returns
AddCulture()
ContextSnapshotBuilder.csAddCurrentDirectory()
ContextSnapshotBuilder.csAddCurrentProcess()
ContextSnapshotBuilder.csAdds information about the current process including command line, memory usage, and processor time.
public ContextSnapshotBuilder AddCurrentProcess()Returns
AddDefault()
ContextSnapshotBuilder.csAdds all available diagnostic information including environment variables, GC, drives, processes, OS details, and more.
public ContextSnapshotBuilder AddDefault()Returns
AddDotnetRuntime()
ContextSnapshotBuilder.csAdds .NET runtime information including runtime identifier and framework description.
public ContextSnapshotBuilder AddDotnetRuntime()Returns
AddDrives()
ContextSnapshotBuilder.csAdds information about all available drives including free space and drive type.
public ContextSnapshotBuilder AddDrives()Returns
AddEnvironmentVariables overloads
AddEnvironmentVariables(EnvironmentVariableTarget)
ContextSnapshotBuilder.csAdds environment variables for the specified target scope, redacting values whose name looks like a secret.
public ContextSnapshotBuilder AddEnvironmentVariables(EnvironmentVariableTarget target = (EnvironmentVariableTarget)0)AddEnvironmentVariables(EnvironmentVariableTarget, Func<string, bool>)
ContextSnapshotBuilder.csAdds environment variables for the specified target scope.
public ContextSnapshotBuilder AddEnvironmentVariables(EnvironmentVariableTarget target, Func<string, bool> shouldRedact)Parameters
targetEnvironmentVariableTarget- The scope to read the variables from.
shouldRedactFunc<string, bool>- Called with each variable name. When it returns
truethe value is replaced byContextSnapshotBuilder.RedactedValue. Pass_ => falseto capture every value verbatim.
Returns
AddGarbageCollector()
ContextSnapshotBuilder.csAdds garbage collector information including generation count and memory usage.
public ContextSnapshotBuilder AddGarbageCollector()Returns
AddHypervisor()
ContextSnapshotBuilder.csAdds hypervisor information when running in a virtualized environment (Windows only).
public ContextSnapshotBuilder AddHypervisor()Returns
AddLocalTimeZone()
ContextSnapshotBuilder.csAdds local time zone information including UTC offset and daylight saving time support.
public ContextSnapshotBuilder AddLocalTimeZone()Returns
AddMachineName()
ContextSnapshotBuilder.csAddNewLine()
ContextSnapshotBuilder.csAddOperatingSystem()
ContextSnapshotBuilder.csAdds operating system information including platform, version, and architecture.
public ContextSnapshotBuilder AddOperatingSystem()Returns
AddPowerManagement()
ContextSnapshotBuilder.csAdds the active power management scheme (Windows only).
public ContextSnapshotBuilder AddPowerManagement()Returns
AddProcessorCount()
ContextSnapshotBuilder.csAdds the number of logical processors available to the current process.
public ContextSnapshotBuilder AddProcessorCount()Returns
AddRuntimeFeatures()
ContextSnapshotBuilder.csAddSecurityProviders()
ContextSnapshotBuilder.csAdds security provider information including antivirus, firewall, and anti-spyware status (Windows only).
public ContextSnapshotBuilder AddSecurityProviders()Returns
AddSpecialFolderPaths()
ContextSnapshotBuilder.csAddThreadPool()
ContextSnapshotBuilder.csAdds thread pool configuration including worker thread and completion port thread counts.
public ContextSnapshotBuilder AddThreadPool()Returns
AddUser()
ContextSnapshotBuilder.csAdds current user information including username and domain.
public ContextSnapshotBuilder AddUser()Returns
AddValue(string, object?)
ContextSnapshotBuilder.csAdds a custom key-value pair to the snapshot.
public ContextSnapshotBuilder AddValue(string key, object? value)BuildSnapshot()
ContextSnapshotBuilder.csBuilds and returns the snapshot as a sorted dictionary of key-value pairs.
public IReadOnlyDictionary<string, object?> BuildSnapshot()IsSecretEnvironmentVariableName(string)
ContextSnapshotBuilder.csDetermines 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.
Constants
| Name | Description |
|---|---|
| RedactedValue | The 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 = "***"