This document describes the architectural design of Audit.NET, focusing on its core components, extension mechanisms, and design patterns. For basic usage patterns, see Getting Started. For details on individual components like AuditScope and AuditEvent, see AuditScope and AuditEvent.
Audit.NET implements a hub-and-spoke architecture where the core framework (Audit.NET) acts as a central orchestrator, and extensions radiate outward as pluggable modules. The architecture separates three concerns:
This separation enables:
Sources: src/Audit.NET/Configuration.cs src/Audit.NET/AuditScope.cs src/Audit.NET/AuditEvent.cs src/Audit.NET/IAuditDataProvider.cs
The AuditScope class is the central orchestrator that controls the lifecycle of an audit event. It is responsible for:
Key characteristics:
IDisposable and IAsyncDisposable for automatic lifecycle managementAuditEvent being trackedIAuditDataProviderSources: src/Audit.NET/AuditScope.cs15-556
AuditEvent is the data model representing audited information. It contains:
| Property | Type | Description |
|---|---|---|
EventType | string | User-defined event category |
Environment | AuditEventEnvironment | Machine, user, calling method info |
StartDate / EndDate | DateTime | Timestamps |
Duration | int | Milliseconds elapsed |
Target | AuditTarget | Old/New state of tracked object |
CustomFields | Dictionary<string, object> | User-defined data |
Comments | List<string> | Audit trail comments |
TimedEvents | List<TimedEvent> | Sub-event checkpoints |
The event is serialized to JSON when persisted by data providers.
Sources: src/Audit.NET/AuditEvent.cs README.md363-404
The storage abstraction interface with three core methods:
Implementations include:
SqlDataProvider, MySqlDataProvider, PostgreSqlDataProviderMongoDataProvider, RedisDataProvider, AzureCosmosDataProviderFileDataProvider, InMemoryDataProvider, NullDataProviderSources: src/Audit.NET/IAuditDataProvider.cs README.md570-628
The Configuration static class provides global settings:
Configuration can be set via:
Configuration.DataProvider = new FileDataProvider()Configuration.Setup().UseFileLogProvider(...).WithCreationPolicy(...)Sources: src/Audit.NET/Configuration.cs12-450
The factory abstraction for creating AuditScope instances. The default implementation is AuditScopeFactory, but users can provide custom implementations to:
OnConfiguring and OnScopeCreated hooksSources: README.md105-172 src/Audit.NET/IAuditScopeFactory.cs
Interaction extensions intercept operations in application frameworks and automatically create AuditScope instances. Each extension follows a pattern:
Examples:
AuditDbContext overrides SaveChanges() to create scope with EntityFrameworkEventAuditApiAttribute filter creates scope with AuditApiActionAuditBehavior attribute intercepts service callsSources: README.md15-28 Diagram 3 from system diagrams
Data providers implement IAuditDataProvider to store events. The framework includes:
| Category | Providers | Key Classes |
|---|---|---|
| SQL | SQL Server, MySQL, PostgreSQL | SqlDataProvider, MySqlDataProvider, PostgreSqlDataProvider |
| NoSQL | MongoDB, Redis, Cosmos DB, Elasticsearch | MongoDataProvider, RedisDataProvider, AzureCosmosDataProvider |
| Cloud | Azure Blobs, Azure Tables, Event Hubs | AzureStorageBlobDataProvider, AzureEventHubsDataProvider |
| Utility | File, InMemory, Null, Activity | FileDataProvider, InMemoryDataProvider, ActivityDataProvider |
Each provider has a fluent configuration API accessible via Configuration.Setup():
Configuration.Setup()
.UseSqlServer(config => config
.ConnectionString("...")
.TableName("AuditEvents")
.IdColumnName("EventId")
.JsonColumnName("JsonData"));
Sources: README.md767-795 src/Audit.NET/ConfigurationApi/Configurator.cs9-200
Wrappers enhance data providers with cross-cutting concerns:
Examples:
Sources: README.md681-763 src/Audit.NET/ConfigurationApi/Configurator.cs63-77
The EventCreationPolicy enum controls when events are inserted/updated:
Configured globally or per-scope:
Sources: src/Audit.NET/EventCreationPolicy.cs README.md306-307 src/Audit.NET/AuditScope.cs254-293
Custom actions execute at four lifecycle points:
Actions can be:
Action<AuditScope>Func<AuditScope, Task>bool to halt subsequent actionsExample:
Sources: src/Audit.NET/Configuration.cs147-350 README.md56-95
The configuration system uses a fluent interface pattern:
Key interfaces:
IConfigurator: Entry point for provider selectionICreationPolicyConfigurator: Policy and action configurationICustomActionConfigurator: Custom action registrationSources: src/Audit.NET/ConfigurationApi/IConfigurator.cs9-142 src/Audit.NET/ConfigurationApi/Configurator.cs9-200
Audit.NET and its extensions support multiple .NET versions using conditional compilation:
| Target Framework | Support |
|---|---|
| .NET Framework | 4.6.2, 4.7.2 |
| .NET Standard | 2.0, 2.1 |
| Modern .NET | 6.0, 7.0, 8.0, 9.0, 10.0 |
Framework-specific features:
net462 and net472Sources: README.md174-188 Directory.Build.props Audit.NET.sln1-204
The core has no compile-time dependencies on extensions or providers. All integration is through abstractions (IAuditDataProvider, IAuditScopeFactory), allowing runtime composition.
Sources: Audit.NET.sln1-204 Diagram 1 from system diagrams
IAuditScopeFactory / AuditScopeFactory for scope creationIAuditDataProvider abstracts storageAuditScope defines lifecycle templateEventCreationPolicy customizes save behaviorPollyDataProvider, LazyDataProvider) enhance base providersConfiguration.Setup() chain for readable configurationUseSqlServer(), UseRedis())Sources: src/Audit.NET/AuditScope.cs15-556 src/Audit.NET/ConfigurationApi/
The Audit.NET architecture achieves extensibility through:
IAuditDataProvider, IAuditScopeFactory) separate concernsAuditScope orchestrates event tracking with well-defined hooksThis design enables adding storage backends, framework integrations, and cross-cutting concerns without modifying the core framework.
Sources: All files referenced above, system architecture diagrams
Refresh this wiki