Class KevlarContext

Namespace
Kevlar
Assembly
Kevlar.dll

Ambient state for a single execution flowing through a shield pipeline. Contexts are created and pooled by Kevlar automatically; user code observes them in strategy callbacks or context-aware execution delegates and never needs to construct or return them. A context is valid only during the current callback or delegate invocation and must never be retained.

public sealed class KevlarContext
Inheritance
KevlarContext
Inherited Members

Remarks

Debug builds of Kevlar enforce that contract: a context that has gone back to the pool throws InvalidOperationException from every public member until it is handed out again. Release builds carry no such check, so a retained context there silently observes another execution's state.

Properties

CancellationToken

The cancellation token for the current scope. Strategies such as timeouts replace this token for the layers beneath them, which is why executed delegates must use the token they are handed rather than a captured one.

public CancellationToken CancellationToken { get; }

Property Value

CancellationToken

IsSynchronous

true when the execution was started through a synchronous Execute call.

public bool IsSynchronous { get; }

Property Value

bool

Properties

Custom properties carried through the execution. The bag is pooled with this context and must not be retained beyond the current callback or delegate invocation.

public KevlarProperties Properties { get; }

Property Value

KevlarProperties

ShieldName

The name of the executing shield, if one was assigned via WithName.

public string? ShieldName { get; }

Property Value

string

StrategyIndex

The zero-based position of the strategy currently executing, or -1 outside a strategy. Nested strategies temporarily replace this value and restore it on return.

public int StrategyIndex { get; }

Property Value

int

TimeProvider

The time provider used for delays, timeouts and time-window calculations.

public TimeProvider TimeProvider { get; }

Property Value

TimeProvider

Methods

RecordEvent(string, KevlarTelemetrySeverity, Exception?, int?, string?)

Records a custom low-cardinality strategy event.

public void RecordEvent(string eventName, KevlarTelemetrySeverity severity = KevlarTelemetrySeverity.Information, Exception? exception = null, int? attemptNumber = null, string? strategyName = null)

Parameters

eventName string

A stable event name from a bounded vocabulary.

severity KevlarTelemetrySeverity

The event severity.

exception Exception

An associated exception, if any. Exception messages are never metric tags.

attemptNumber int?

The zero-based attempt number, or null to use the active retry attempt.

strategyName string

A stable low-cardinality custom strategy name.