NB.EfCore.AuditLog 1.0.0

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

EfCore.AuditLog

NuGet License: MIT

Audit logging for Entity Framework Core using SaveChangesInterceptor.

It tracks INSERT, UPDATE, and DELETE operations, stores old and new value snapshots as JSON, and can attach user, tenant, source, and trace metadata through a pluggable provider.

Features

  • Zero changes to your existing SaveChanges() and SaveChangesAsync() calls
  • Opt-in or opt-out auditing with [Auditable] and ignore rules
  • Property-level filtering with [AuditIgnore] or global ignored property names
  • User-context enrichment through IAuditUserProvider
  • Same-transaction persistence so audit rows are committed atomically with the business change
  • Support for insert, update, and delete snapshots
  • Tested against SQLite with coverage for core interceptor behavior

Installation

Install-Package EfCore.AuditLog

Or with the .NET CLI:

dotnet add package EfCore.AuditLog

Requirements

  • .NET 8.0
  • EF Core 7.0 or later

Quick Start

1. Configure audit options and services

var auditOptions = new AuditOptions
{
    Mode = AuditMode.OptIn,
    TableName = "audit_logs"
    // Schema = "audit"
};

auditOptions.IgnoreProperties("PasswordHash", "SecurityStamp");

builder.Services.AddAuditLogging(options =>
{
    options.Mode = auditOptions.Mode;
    options.TableName = auditOptions.TableName;
    options.Schema = auditOptions.Schema;
    options.EnableMultiTenancy = auditOptions.EnableMultiTenancy;
    options.IgnoreProperties("PasswordHash", "SecurityStamp");
});

builder.Services.AddScoped<IAuditUserProvider, MyAuditUserProvider>();

builder.Services.AddDbContext<AppDbContext>((sp, options) =>
{
    options.UseSqlServer(connectionString);
    options.AddAuditInterceptor(sp);
});

2. Configure your DbContext

public class AppDbContext : DbContext
{
    private readonly AuditOptions _auditOptions;

    public AppDbContext(DbContextOptions<AppDbContext> options, AuditOptions auditOptions)
        : base(options)
    {
        _auditOptions = auditOptions;
    }

    public DbSet<AuditLogEntry> AuditLogs => Set<AuditLogEntry>();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyAuditLogConfiguration(_auditOptions);
    }
}

3. Mark entities for auditing

[Auditable]
public class Order
{
    public int Id { get; set; }
    public string CustomerName { get; set; } = null!;
    public decimal Total { get; set; }

    [AuditIgnore]
    public string? InternalNotes { get; set; }
}

4. Implement IAuditUserProvider

using System.Security.Claims;
using EfCore.AuditLog.Abstractions;

public sealed class MyAuditUserProvider : IAuditUserProvider
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public MyAuditUserProvider(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public string? GetUserId()
        => _httpContextAccessor.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;

    public string? GetUserEmail()
        => _httpContextAccessor.HttpContext?.User?.FindFirst(ClaimTypes.Email)?.Value;

    public string? GetTenantId()
        => _httpContextAccessor.HttpContext?.Request.Headers["X-Tenant-Id"].FirstOrDefault();

    public string? GetSource() => "MyApp";

    public string? GetTraceId() => _httpContextAccessor.HttpContext?.TraceIdentifier;
}

What Gets Logged

Operation OldValues NewValues
INSERT null All non-ignored properties
UPDATE Modified properties only, using original values Modified properties only, using current values
DELETE All non-ignored properties null

Each audit row stores:

  • Table name
  • Record identifier
  • Operation type
  • JSON snapshots of old and new values
  • User, email, tenant, source, and trace information when available
  • UTC timestamp of the change

Configuration Options

services.AddAuditLogging(options =>
{
    options.TableName = "audit_logs";
    options.Schema = null;
    options.Mode = AuditMode.OptIn;
    options.EnableMultiTenancy = false;

    options.IgnoreEntities(typeof(TempLog));
    options.IgnoreProperties("PasswordHash", "SecretKey");
});

Notes

  • The package persists audit rows in the same save pipeline as the business operation.
  • This favors consistency and atomicity over fully decoupled background logging.
  • For inserted entities with database-generated keys, the interceptor performs a follow-up save to populate the final RecordId.
  • If you customize TableName or Schema, pass the same AuditOptions instance into ApplyAuditLogConfiguration(...) in your DbContext.

Release Notes

See CHANGELOG.md for version history.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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

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.0 625 4/19/2026

Initial public release. Includes SaveChangesInterceptor-based audit logging, opt-in and opt-out entity selection, property filtering, user context integration, and tests for insert, update, delete, and configuration scenarios.