Swevo.AutoAudit 1.0.1

Prefix Reserved
dotnet add package Swevo.AutoAudit --version 1.0.1
                    
NuGet\Install-Package Swevo.AutoAudit -Version 1.0.1
                    
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="Swevo.AutoAudit" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Swevo.AutoAudit" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Swevo.AutoAudit" />
                    
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 Swevo.AutoAudit --version 1.0.1
                    
#r "nuget: Swevo.AutoAudit, 1.0.1"
                    
#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 Swevo.AutoAudit@1.0.1
                    
#: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=Swevo.AutoAudit&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Swevo.AutoAudit&version=1.0.1
                    
Install as a Cake Tool

Swevo.AutoAudit

NuGet Build License: MIT

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.


Installation

dotnet add package Swevo.AutoAudit

Requires EF Core 7+.


Quick Start

1. Mark your entity

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; }
}

2. Register the interceptor

// 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.


How It Works

Event Fields Updated
Added CreatedAt, CreatedBy, UpdatedAt, UpdatedBy
Modified UpdatedAt, UpdatedBy

CreatedAt and CreatedBy are never overwritten on subsequent saves.


Generated Types

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

Diagnostics

ID Severity Description
AUDIT001 Error Class must be partial to use [Auditable]

Compatibility

Dependency Version
EF Core 7.0+
.NET net7.0+
C# 10+

License

MIT © 2025 Justin Bannister

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 124 6/26/2026
1.0.0 94 6/26/2026

1.0.0: Initial release. Generates CreatedAt, UpdatedAt, CreatedBy, UpdatedBy audit fields and AuditInterceptor for EF Core.