Auditing with H.Necessaire: Entries, Snapshots and History
Record who changed what with audit entries and snapshots, then browse the history with AuditSearchFilter.
Applies to H.Necessaire 4.0.5.
Auditing answers “who changed what, and what did it look like then?” An audit entry in H.Necessaire is a small block of metadata plus a full snapshot of the audited object, stored separately so you can browse the history cheaply and load a snapshot only when you need it.
Install
dotnet add package H.NecessaireThe service
interface ImAnAuditingService
{
Task Append<T>(ImAnAuditEntry metadata, T objectSnapshot);
Task<ImAnAuditEntry[]> Browse(AuditSearchFilter filter);
}Writing an entry
Build the metadata from the object itself with ToAuditMeta, then append it with a snapshot:
ImAnAuditingService audit = deps.Get<ImAnAuditingService>();
// Order implements IGuidIdentity, so its ID becomes the audited object ID
await audit.Append(
order.ToAuditMeta<Order, Guid>(AuditActionType.Modify, doneBy: currentUser),
order
);
// For objects without an ID property, pass the ID yourself
await audit.Append(settings.ToAuditMeta("app-settings", AuditActionType.Modify, currentUser), settings);The action types are Create, Read, Modify and Remove. doneBy is any IDentity (ID, tag and display name), and may be null. The service also stamps each entry with the current AppVersion from the version provider, so you can tell which release made the change.
The entry
interface ImAnAuditEntry : IGuidIdentity
{
string AuditedObjectType { get; } // type name
string AuditedObjectID { get; }
DateTime HappenedAt { get; }
IDentity DoneBy { get; }
AuditActionType ActionType { get; }
Version AppVersion { get; }
Task<T> GetObjectSnapshot<T>(); // loads the snapshot on demand
}Browsing history
ImAnAuditEntry[] history = await audit.Browse(new AuditSearchFilter
{
AuditedObjectTypes = new[] { typeof(Order).TypeName() },
AuditedObjectIDs = new[] { order.ID.ToString() },
ActionTypes = new[] { AuditActionType.Create, AuditActionType.Modify },
FromInclusive = DateTime.UtcNow.AddDays(-30),
});
foreach (ImAnAuditEntry entry in history)
{
Order snapshot = await entry.GetObjectSnapshot<Order>();
Console.WriteLine($"{entry.HappenedAt:u} {entry.ActionType} by {entry.DoneBy?.DisplayName}");
}AuditSearchFilter is a normal H.Necessaire filter, so it also carries paging and sorting. The sortable fields are ID, ActionType, AuditedObjectID, AuditedObjectType and HappenedAt. You can also filter by who did it with DoneBy (IDs, tags or display names) and by an upper date bound with ToInclusive.
GetObjectSnapshot<T>() reads the stored snapshot only when called, and JsonAuditEntry caches it after the first read. If the snapshot can't be loaded or parsed it returns default(T) rather than throwing.
Where entries are stored
Every provider stores the metadata and the snapshot separately:
| Provider | Layout |
|---|---|
| Runtime default (file system) | Metadata as JSON files, snapshots as ObjectSnapshots/<entryId>.json under the audit storage folder. |
| SQLite | A metadata table and a payload table holding the snapshot JSON. |
| SQL Server, RavenDB, Cosmos DB, Firestore | Each has its own audit resource implementing ImAnAuditingService. |
Registering a database provider swaps the implementation without touching your code. One behavioural difference to be aware of: Append returns a plain Task, and the SQLite implementation throws if saving the metadata or snapshot fails, so wrap audit calls in your normal error handling if auditing must never break the main operation (HSafe.Run works well here).
What the library audits for you
The sync pipeline writes audit entries as it works: the sync use case records a Create for each received request, and the daemon records a Remove when it finishes processing or exiling one. It is a handy trail when you debug why an entity didn't arrive.
Comparing snapshots
The core includes AuditComparison<T> and AuditObjectDifference (property path, preceding and following values) as the models for presenting a before/after diff of two snapshots, which pair naturally with GetObjectSnapshot.