RG.AuditTrail 1.1.0

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

RG.AuditTrail

โœจ Features

  • Insert / Update / Delete actions
  • Old Values and New Values
  • Logged-in Username from JWT token
  • Timestamp and Table Name
  • Correlation ID tracking
  • User, IP & request tracking
  • Plug & Play integration
  • Works with custom DbContext

๐Ÿ“ฆ Installation

dotnet add package RG.AuditTrail

RG.AuditTrail with JWT Authentication }

๐Ÿ”น Step 1: Register Services

๐Ÿ“‚ File: Program.cs

csharp

NameSpaces in PROGRAM.CS

๐Ÿ“‚ File: Program.cs

using AuditTrail.core.Extensions;
using AuditTrail.core.Helpers;
using AuditTrail.core.Interceptor;
using AuditTrail.core.Interfaces;
using AuditTrail.core.Services;
using AuditTrailWithJWT.Data;

Database Connection in PROGRAM.CS

๐Ÿ“‚ File: Program.cs

builder.Services.AddDbContext<AppDbContext>((provider, options) =>
{
    options.UseSqlServer(
        builder.Configuration.GetConnectionString("Connection"))
    .UseAuditTrail(provider); 
});

Required Services in PROGRAM.CS

๐Ÿ“‚ File: Program.cs

builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<HttpContextHelper>();
builder.Services.AddScoped<IAuditService, AuditService>();
builder.Services.AddScoped<AuditSaveChangesInterceptor>();
builder.Services.AddScoped<IAuditDbContext>(sp =>sp.GetRequiredService<AppDbContext>());

Add Middleware (IMPORTANT ORDER) in PROGRAM.CS

๐Ÿ“‚ File: Program.cs

app.UseAuthentication();
app.UseAuthorization();

app.UseCorrelationId();   // ๐Ÿ”ฅ MUST be first
app.UseAuditTrail();      // ๐Ÿ”ฅ Then audit logging

๐Ÿ”น Step 2: Create AuditLog Table Model create a model otherwise use service on PROGRAM.CS file (Optional if not exists)

builder.Services.AddScoped<IAuditDbContext>(sp โ‡’sp.GetRequiredService<AppDbContext>());

๐Ÿ“‚ File: Models/AuditLog.cs

public class AuditLog
{
    public long Id { get; set; }
    public string? UserId { get; set; }
    public string? Action { get; set; }
    public string? TableName { get; set; }
    public string? RecordId { get; set; }
    public string? OldValues { get; set; }
    public string? NewValues { get; set; }
    public DateTime CreatedAt { get; set; }
    public string? IpAddress { get; set; }
    public string? CorrelationId { get; set; }
}

๐Ÿ”น Step 3: Implement DbContext

๐Ÿ“‚ File: Data/AppDbContext.cs

using AuditTrail.core.Interfaces;
using AuditTrail.core.Models;

public class AppDbContext : DbContext, IAuditDbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options)
    {
    }

    public DbSet<AuditLog> AuditLogs { get; set; }   
}

๐Ÿ”น Step 4: JWT Claims Required

var claims = new[]
{
    new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
    new Claim(ClaimTypes.Name, user.Name),
    new Claim(ClaimTypes.Role, user.Role)
};


RG.AuditTrail (with out JWT Authentication) }

๐Ÿ”น Step 1: Program.cs

Requied NAMESPACES in PROGRAM.CS ๐Ÿ“‚ File: Program.cs

using ActivitylogTest.Data;
using AuditTrail.core.Extensions;
using AuditTrail.core.Interfaces;

Required Database Connection in PROGRAM.CS ๐Ÿ“‚ File: Program.cs

builder.Services.AddDbContext<AppDbContext>((provider, options) =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("SimpleConn"))
           .UseAuditTrail(provider);
});

Required Services in PROGRAM.CS ๐Ÿ“‚ File: Program.cs

builder.Services.AddScoped<IAuditDbContext>(provider => provider.GetRequiredService<AppDbContext>());
builder.Services.AddAuditTrail();

Required middleware in PROGRAM.CS ๐Ÿ“‚ File: Program.cs

app.UseCorrelationId();
app.UseAuditTrail();

๐Ÿ”น Step 2: Add this line in dbContext File

๐Ÿ“‚ File: Data/AppDbContext.cs

    using AuditTrail.core.Interfaces;
    using AuditTrail.core.Models;

    public class AppDbContext : DbContext, IAuditDbContext
          {
              public AppDbContext(DbContextOptions<AppDbContext> options)
                  : base(options)
              {
              }
          
              public DbSet<AuditLog> AuditLogs { get; set; }   
          }

๐Ÿงช Example Flow

  1. User sends request

  2. CorrelationId middleware generates unique ID

  3. Audit middleware captures:

    • User
    • IP Address
    • API endpoint
    • Status code
  4. Data saved into AuditLogs table


๐Ÿ“Š Sample Output (Database)

Id UserId Action TableName IpAddress CorrelationId
1 admin GET-200 /api/user 127.0.0.1 abc-123

โš ๏ธ Important Notes

  • Middleware order must be correct
  • DbContext must implement IAuditDbContext
  • CorrelationId helps track request across services

๐Ÿ“Œ Requirements

  • .NET 8+
  • Entity Framework Core

๐Ÿ‘จโ€๐Ÿ’ป Author

Raj Gupta


โญ Support

If you like this package, give it a โญ on GitHub!

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.1.0 131 4/27/2026
1.0.0 107 4/27/2026