Athenex.Whimclean.Auditing 2.0.0

Prefix Reserved
dotnet add package Athenex.Whimclean.Auditing --version 2.0.0
                    
NuGet\Install-Package Athenex.Whimclean.Auditing -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Athenex.Whimclean.Auditing" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Athenex.Whimclean.Auditing" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Athenex.Whimclean.Auditing" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Athenex.Whimclean.Auditing --version 2.0.0
                    
#r "nuget: Athenex.Whimclean.Auditing, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Athenex.Whimclean.Auditing@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Athenex.Whimclean.Auditing&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Athenex.Whimclean.Auditing&version=2.0.0
                    
Install as a Cake Tool

Athenex.Whimclean.Auditing

ภาษาไทย

An audit log for Whimclean applications: every command marked as audited is recorded with its actor, target, outcome and the field changes its domain events reported, whether it succeeded, failed or threw. Entries are written outside the command's unit of work, so a failed attempt still leaves a trace while its own changes roll back.

Install

dotnet add package Athenex.Whimclean.Auditing
dotnet add package Athenex.Whimclean.Auditing.AspNetCore   # in the API host

The package depends on Athenex.Whimclean.Application, Athenex.Whimclean.Domain and Mediator.Abstractions; the application supplies the Mediator source generator as it does for Athenex.Whimclean.Mediator.

Mark commands as audited

using Athenex.Whimclean.Auditing;
using Mediator;

public sealed record SuspendTenantCommand(Guid TenantId, string Reason)
    : ICommand<Result>, IAuditedCommand
{
    public AuditDescriptor Describe() => new(
        Action: "Tenant.Suspend",
        EntityType: "Tenant",
        EntityId: TenantId.ToString(),
        TenantId: TenantId,
        Reason: Reason);
}

AuditDescriptor takes Action and EntityType, then optional EntityId, EntityReference, TenantId, Reason and Metadata (a dictionary serialized as a JSON object). A command that creates an entity leaves EntityId null; the behavior fills it from a successful Result<T> whose value is a Guid, a string or a primitive such as a number. TenantId must be of the tenant id type registered with AddAuditing; a value of another type is ignored.

Report changes from domain events

Handlers derived from DomainEventHandler<TEvent> (package Athenex.Whimclean.Mediator) add what they know to the scoped collector; the entry picks it up after the command commits:

using Athenex.Whimclean.Auditing;
using Athenex.Whimclean.Mediator;

public sealed class TenantRenamedAuditHandler(IAuditChangeCollector collector)
    : DomainEventHandler<TenantRenamed>
{
    protected override ValueTask HandleAsync(TenantRenamed domainEvent, CancellationToken cancellationToken)
    {
        collector.Add("name", domainEvent.OldName, domainEvent.NewName);
        collector.SetEntityReference(domainEvent.Slug);
        return ValueTask.CompletedTask;
    }
}

IAuditChangeCollector offers Add(field, oldValue, newValue), SetEntityReference(reference) and SetTenantId(tenantId). Values the descriptor already names win over the collector's.

Implement the writer

The entry must commit on its own connection. A DbContext factory does that; the table is mapped and migrated by the application's main context:

using Athenex.Whimclean.Auditing;
using Microsoft.EntityFrameworkCore;

public sealed class AuditLogWriter(IDbContextFactory<AuditLogDbContext> factory)
    : IAuditLogWriter<Guid, Guid>
{
    public async ValueTask WriteAsync(AuditLog<Guid, Guid> entry, CancellationToken cancellationToken = default)
    {
        await using var context = await factory.CreateDbContextAsync(cancellationToken);
        context.Add(entry);
        await context.SaveChangesAsync(cancellationToken);
    }
}

Map Changes and Metadata to a JSON column and give the text columns the AuditLog<,>.Maximum…Length sizes; the entity trims and truncates to those, so writes never fail on length:

Constant Length
MaximumActorNameLength 200
MaximumActionLength 100
MaximumEntityTypeLength 100
MaximumEntityIdLength 100
MaximumEntityReferenceLength 300
MaximumReasonLength 1000
MaximumErrorCodeLength 200
MaximumCorrelationIdLength 100
MaximumIpAddressLength 45
MaximumUserAgentLength 500

The JSON documents are produced by AuditJson with web defaults (camelCase) and nulls kept, so a change to null stays visible.

Register

using Athenex.Whimclean.Auditing;
using Athenex.Whimclean.Auditing.AspNetCore;
using Athenex.Whimclean.Mediator;

services
    .AddAuditing<Guid, Guid>()          // before AddUnitOfWorkBehavior
    .AddMediatorDomainEventDispatcher()
    .AddUnitOfWorkBehavior();
services.AddScoped<IAuditLogWriter<Guid, Guid>, AuditLogWriter>();
services.AddHttpAuditRequestContext();  // Athenex.Whimclean.Auditing.AspNetCore

AddAuditing must come before AddUnitOfWorkBehavior: Mediator runs behaviors in registration order, and the audit behavior has to wrap the unit of work to see the committed outcome. The audit write itself is not cancellable: an outcome that happened is always recorded, and an exception from the handler is recorded and then rethrown.

AddAuditing<TUserId, TTenantId> registers the scoped AuditCommandBehavior<,>, AuditChangeCollector and AuditLogRecorder<TUserId, TTenantId>, plus TimeProvider.System and a NullAuditRequestContext unless one of each is already registered. The type arguments are the local user id and the tenant id, both value types. The IAuditLogWriter<TUserId, TTenantId> is registered separately. Outside an HTTP request the NullAuditRequestContext stays in place and an entry whose caller is unknown is attributed to system.

What an entry holds

Field Source
Id, OccurredAt a version 7 Guid and the TimeProvider clock, normalized to UTC
ActorId, ActorName, ActorType ICurrentUser<TUserId> and the request context's actor name, falling back to the user id as text; system when neither knows the caller
Action, EntityType, EntityId, EntityReference, TenantId, Reason, Metadata the descriptor, completed by the collector
Result, ErrorCode AuditResult.Success, or AuditResult.Failed with the failed Result's error code or the exception type name
Changes the collector's changes as a JSON array, on success only
CorrelationId, IpAddress, UserAgent the request context

Repository and full architecture guide: https://github.com/athene-x/Whimclean

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Athenex.Whimclean.Auditing:

Package Downloads
Athenex.Whimclean.Auditing.AspNetCore

Supplies the audit log with what the current HTTP request knows about the caller: display name from the principal, correlation id, remote address and user agent.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 115 11/17/2026

First release: audited commands, outcome recording outside the unit of work, change collection from domain events, and the audit entry entity with its length limits.