Skip to content

Repository files navigation

🛡️ EntityAudit - Automatic Audit Trails for EF Core

NuGet Version GitHub License .NET Version

A lightweight library to automatically track entity changes (audit logs) in EF Core 9.0 with zero configuration.


✨ Features

  • ✅ Automatic audit fields: CreatedAt, CreatedBy, UpdatedAt, UpdatedBy
  • ✅ EF Core 9.0 optimized
  • ✅ Supports custom user tracking via ICurrentUserService
  • ✅ Minimal boilerplate (just inherit AuditableEntity)

🚀 Quick Start

📦 Installation

dotnet add package Softylines.EntityAudit

🧑‍💻 Usage

1️⃣ Implement ICurrentUserService

public class CurrentUserService : ICurrentUserService
{
    private readonly IHttpContextAccessor _httpContextAccessor;

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

    public string GetCurrentUser()
    {
        return _httpContextAccessor.HttpContext?.User?.Identity?.Name ?? "System";
    }
}

➡️ Register it in Program.cs:

builder.Services.AddScoped<ICurrentUserService, CurrentUserService>();
builder.Services.AddHttpContextAccessor();

2️⃣ Configure DbContext with EntityAudit

builder.Services.AddDbContext<AppDbContext>(options =>
{
    options.UseNpgsql(builder.Configuration.GetConnectionString("Postgres"))
           .AddEntityAuditInterceptor();
});

3️⃣ Create an Auditable Entity

public class Product : AuditableEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

✅ All audit fields are inherited from AuditableEntity and automatically handled.


4️⃣ Run Migrations

dotnet ef migrations add Init
dotnet ef database update

EF Core will add audit fields like:

table.Column<string>(name: "CreatedBy", nullable: true),
table.Column<DateTime>(name: "CreatedAt", nullable: false),
table.Column<string>(name: "UpdatedBy", nullable: true),
table.Column<DateTime>(name: "UpdatedAt", nullable: true),

✅ These fields are automatically filled in based on the user context!


📚 Summary

EntityAudit gives you:

  • 🔒 Transparent change tracking
  • 👤 Full user-based auditing
  • 🧼 Clean and minimal setup

Ready to audit your entities? Install it now and keep your data history clean and secure.


🔗 Links

About

A lightweight .NET library to **automatically track entity changes** (audit logs) in EF Core 9.

Resources

Stars

6 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages