Core Concepts — AuditRecord, TenantId & Integrity Chain
EricksonLopez.Auditing models every audit entry as an immutable event record bound to an explicit tenant boundary and optional cryptographic chain.
Immutable AuditRecord & AuditRecordBuilder
Section titled “Immutable AuditRecord & AuditRecordBuilder”AuditRecord is a sealed record enforcing required structural fields (Id, OccurredAt, Actor, Action, Resource, Outcome, Context) alongside optional Changes, ErrorCode, IntegrityHash, and PreviousHash:
AuditId.NewId(): Generates a time-orderedUUIDv7Guidfor sequential primary-key locality in B-tree database indexes.AuditRecordBuilder: Fluent builder validating actor, action, resource, and outcome invariants while pulling ambientAuditScopevalues automatically (TenantId,CorrelationId, metadata).- Length guardrails (
v2.0.0): Strings exceedingMaxStringLengthare truncated deterministically before persistence.
Since v2.0.0 (BC-001–BC-003), AuditContext, AuditQuery, and IAuditIntegrityProvider enforce TenantId instead of raw strings:
TenantIdwraps a non-empty trimmed tenant identifier (ArgumentException.ThrowIfNullOrWhiteSpace) with implicit conversions to and fromstring.SystemTenantIdis astatic readonly TenantId(changed fromconst stringinv2.0.0).AuditScope.Begin()stores ambient context inAsyncLocal<T>and restores the outer scope cleanly onDispose().
HmacAuditIntegrityService computes each record’s IntegrityHash over length-prefixed UTF-8 field encodings (v2.0.0 BC-006) plus the tenant’s previous IntegrityHash:
- Length-prefix framing prevents canonicalization ambiguity when field values contain delimiter characters.
- Database providers index
(tenant_id, previous_hash)and supportidempotency_keydeduplication to prevent chain forks under concurrent retries. IAuditStoreexposesAppendAsync,AppendBatchAsync, cursor-basedQueryAsync(AuditQuery)(ContinuationToken/NextPageToken), andStreamAsync(AuditQuery).