Appearance
Transactional audit trail
An audited act that uses this path has one rule: no record, no act. The record is staged in the outbox of the act's own transaction, so a rollback leaves neither, and a process that dies after the commit still delivers it.
The design and what is still open are in ADR 0015.
Set up
The context that saves the act must map the outbox tables, as described in Transactional outbox. Add the package Gryd.Observability.EntityFrameworkCore, then compose the writer and the delivery:
csharp
services.AddAuditTrail<OrdersDbContext>();
services.AddOutboxMessaging(outbox =>
{
outbox.AddProducer("orders", provider => CreateDrainContext(provider));
outbox.AddAuditSource("orders", provider => CreateDestination(provider));
});
services.AddOutboxWorker();Register every route before the worker starts. A message with no route is not delivered.
The default delivery writes through the registered IAuditStore, which must be durable and idempotent by AuditEntry.Id. EfCoreAuditStore from GrydAudit is. With NullAuditStore, a delivery fails with AUDIT_RECORD_STORE_NOT_DURABLE instead of losing the line.
To write the row in the receipt's transaction, use GrydAudit's consumer with the audit database as the destination:
csharp
outbox.AddAuditSource<AuditDbContext, AuditLogRecordConsumer>("orders", CreateAuditContext);An application layer that must not know the EF context names a marker instead: services.AddAuditTrail<IOrdersUnitOfWork, OrdersDbContext>().
Stage a record
csharp
public sealed class CancelOrderHandler(OrdersDbContext db, IAuditTrail<OrdersDbContext> trail, ICurrentUserService user)
{
public async Task HandleAsync(Order order, string reason, CancellationToken ct)
{
order.Cancel(reason);
trail.Stage(AuditEntry.Builder(nameof(Order), order.Id.ToString())
.WithTenant(order.TenantId)
.WithUser(user.UserId)
.WithChangeType(EntityChangeType.Updated)
.WithCategory(AuditCategories.Data)
.WithAction("order.cancelled")
.WithOutcome(AuditOutcomes.Success)
.WithChange(nameof(Order.Status), "Open", "Cancelled")
.WithMetadata("Reason", reason)
.Build());
await db.SaveChangesAsync(ct);
}
}Stagenever saves. The nextSaveChangeswrites the act and the record, or neither.- A failure in
Stagemust fail the act, so do not catch it. - Mask personal data before calling it. The entry enters the outbox as received.
- The tenant must be the current one. A group scope is refused.
- Without a tracked entity, as in a bulk
ExecuteUpdate, stage one record that describes the operation in an open transaction, then save.
Platform records
For an act that belongs to no tenant, use AddPlatformAuditTrail<TContext>(), IPlatformAuditTrail<TContext> and outbox.AddPlatformAuditSource(...). The entry has no tenant, and the act runs under TenantSnapshot.ForPlatformExecution(). The host authorizes who can reach that frame.
What to expect
- Delay. A record is readable after delivery, not right after the act.
- Worker. Without one, acts succeed and records wait in the outbox. Alert on the age of the backlog.
- At least once. A delivery can repeat. The entry id makes it one row.
- No order. Each record is its own stream, so one failure does not hold the others.
- Reading.
IAuditLogRepository, from GrydAudit, searches under the tenant filter and has no permission of its own. Protect your route with your own permission.
An event with no act to join, such as a refused attempt, still uses IAuditStore directly.
What uses this path
GrydFiles and the GrydAuth membership trail stage through it. Other Auth writes, and the audit interceptor, still write after the commit. See the delivery status in the ADR.
Upgrading to 11.1
There is no migration and no signature change. Pending messages of the older kinds are still delivered.
- Hosts with GrydFiles or
AddAuthMembershipAuditIntegration: the new route is registered by those calls. - Hosts that use
RequireRouteAdmissionon those producers: also callAdmitRouteforaudit.recordv1. Until then, the new records stay pending. - Workers: stop the old ones before deploying. An old worker has no route for the new kind.
- Metadata of the Auth membership rows:
producerId,consumerId,messageId,causationIdandcorrelationIdare no longer recorded, because they exist only at delivery.