Configuration & Integrations — Auditing
AddAuditing composes persistence stores, actor resolvers, enrichers, buffering, integrity hashing, and security alerting through IAuditBuilder.
using System;using System.Data;using EricksonLopez.Auditing;using EricksonLopez.Auditing.PostgreSql;using Microsoft.Extensions.DependencyInjection;
namespace Example.AuditingConfig;
public static class AuditingRegistration{ public static void Configure( IServiceCollection services, Func<IDbConnection> connectionFactory, IAuditIntegrityProvider integrityProvider) { services.AddSingleton(integrityProvider);
services.AddAuditing(config => { config.DefaultFailureBehavior = AuditFailureBehavior.FailClosed; config.MaxStringLength = 4000; }) .UsePostgreSql(pg => { pg.ConnectionFactory = connectionFactory; pg.Schema = "audit"; pg.Table = "records"; }) .EnableIntegrityChain(); }}Pass a Func<IDbConnection> to pg.ConnectionFactory (for example, () => new NpgsqlConnection(connectionString)), while AuditConfiguration controls DefaultFailureBehavior, MaxStringLength (clamped at 8192), BatchSize, and SanitizeContextMetadata.
Integrity Chain Requirement: Calling
.EnableIntegrityChain()registersIAuditHashAlgorithm(HmacSha256AuditHashAlgorithm) andHmacAuditIntegrityService, and wraps the registeredIAuditStoreinIntegrityAuditStoreDecorator. BecauseHmacAuditIntegrityServicerequiresIAuditIntegrityProvider, you must register anIAuditIntegrityProviderinIServiceCollection(for example,TestAuditIntegrityProviderfromEricksonLopez.Auditing.Testingin tests,PostgreSqlKmsAuditKeyProviderinEricksonLopez.Auditing.PostgreSql,AzureKeyVaultIntegrityProviderin the unpublished localEricksonLopez.Auditing.AzureKeyVaultsatellite, or a custom implementation); otherwiseValidateOnBuildor resolvingIAuditStore/IAuditLogger<T>throwsInvalidOperationException.
Key IAuditBuilder methods in EricksonLopez.Auditing:
UseStore<TStore>()— register customIAuditStoreimplementation.UseActorProvider<TProvider>()— resolve authenticatedAuditActorautomatically from HTTP or worker context.AddEnricher<TEnricher>()— register anIAuditEnricher(EnrichAsync) to augment audit records (redaction/sensitivity rules are applied byIAuditSensitivityPipeline.ApplyAsync/SanitizeAsync).EnableBuffering(...)— batch writes asynchronously under burst traffic.EnableSecurityAlerts()/AddSecurityAlertSink<TSink>()— forward anomalous failure spikes to security sinks.
Published Providers vs Local-Only Satellites
Section titled “Published Providers vs Local-Only Satellites”| Category | Packages | Notes |
|---|---|---|
Relational AOT-ready (2.0.0) |
PostgreSql, SqlServer, MySql, Oracle, Sqlite |
Regex-validated SQL identifiers; (tenant_id, previous_hash) index |
Dapper (2.0.0) |
EricksonLopez.Auditing.Dapper |
Marked IsAotCompatible=true in .csproj; underlying Dapper v2.1.79 uses reflection emit |
Reflection ORMs (2.0.0) |
EntityFrameworkCore, MongoDb |
Explicitly IsAotCompatible=false (Incompatible AOT) |
Telemetry & Testing (2.0.0) |
OpenTelemetry, Testing |
Activity/metrics instrumentation and InMemoryAuditStore |
Unpublished Local (3.1.0) |
Analyzers, AzureKeyVault, Outbox, Serilog |
Local workspace only — not available via nuget.org |