[](https://www.nuget.org/packages/Swevo.AutoAudit/) Build License: MIT .NET 10 Ready
Compile-time audit field generation for EF Core entities using Roslyn source generators. Add [Auditable] to any partial entity class and get CreatedAt, UpdatedAt, CreatedBy, and UpdatedBy properties β plus a ready-to-use AuditInterceptor β all generated at build time. Zero reflection, AOT-safe, no runtime overhead.
dotnet add package Swevo.AutoAudit
Requires EF Core 7+.
using Swevo.AutoAudit; [Auditable] public partial class Order { public int Id { get; set; } public string? Description { get; set; } }
The generator adds these properties automatically β no base class required:
// Generated: partial class Order : IAuditableEntity { public DateTimeOffset CreatedAt { get; set; } public DateTimeOffset UpdatedAt { get; set; } public string? CreatedBy { get; set; } public string? UpdatedBy { get; set; } }
// Program.cs / Startup.cs builder.Services.AddDbContext<AppDbContext>((sp, options) => { options.UseSqlServer(connectionString); // Optional: supply the current user identity var httpContext = sp.GetService<IHttpContextAccessor>(); options.AddAuditInterceptor(() => httpContext?.HttpContext?.User?.Identity?.Name); });
That's it. CreatedAt/CreatedBy are set on insert; UpdatedAt/UpdatedBy are refreshed on every update.
| Event | Fields Updated |
|---|---|
Added |
CreatedAt, CreatedBy, UpdatedAt, UpdatedBy |
Modified |
UpdatedAt, UpdatedBy |
CreatedAt and CreatedBy are never overwritten on subsequent saves.
The generator emits three shared sources into your project's Swevo.AutoAudit namespace:
| Type | Description |
|---|---|
[Auditable] |
The attribute itself |
IAuditableEntity |
Interface implemented by all auditable entities |
AuditInterceptor |
SaveChangesInterceptor that applies audit values |
AuditInterceptorExtensions |
AddAuditInterceptor() extension for DbContextOptionsBuilder |
| ID | Severity | Description |
|---|---|---|
AUDIT001 |
Error | Class must be partial to use [Auditable] |
| Dependency | Version |
|---|---|
| EF Core | 7.0+ |
| .NET | net7.0+ |
| C# | 10+ |
π Full suite overview: swevo.github.io
| Package | Description |
|---|---|
| AutoLog.Generator | Compile-time high-performance logging β [Log(Level, Message)] generates LoggerMessage.Define. AOT-safe. |
| AutoHttpClient.Generator | Compile-time typed HTTP client β [HttpClient] on an interface generates a strongly-typed client. AOT-safe Refit alternative. |
| AutoDispatch.Generator | Compile-time CQRS dispatcher β [Handler] generates a strongly-typed IDispatcher. No MediatR, no reflection. |
| AutoWire | Compile-time DI auto-registration β [Scoped]/[Singleton]/[Transient] generates IServiceCollection registration code. |
| AutoMap.Generator | Compile-time object mapping with generated extension methods. AOT-safe AutoMapper alternative. |
| Package | Downloads | Description |
|---|---|---|
| Swevo.AutoBus | Downloads | Free, MIT-licensed in-process message bus for |
| Swevo.AutoBus.RabbitMQ | Downloads | RabbitMQ transport for AutoBus |
| Swevo.AutoAssert | Downloads | Free, MIT-licensed fluent assertions for |
| Swevo.AutoAuth | Downloads | A free, MIT-licensed fluent configuration wrapper around OpenIddict for building OAuth2/OIDC token servers in ASP |
| Swevo.AutoResult | Downloads | Compile-time Result monad for |
| Swevo.AutoGuard | Downloads | Compile-time guard clauses for |
| Swevo.AutoImage | Downloads | A free, MIT-licensed fluent image processing wrapper around SkiaSharp for |
| Swevo.AutoFeatureFlag | Downloads | Compile-time feature flag stubs for |
| Swevo.AutoTestData | Downloads | Compile-time test data builders for |
MIT Β© 2025 Justin Bannister