AlirezaFlh.AudiLogging 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package AlirezaFlh.AudiLogging --version 1.0.2                
NuGet\Install-Package AlirezaFlh.AudiLogging -Version 1.0.2                
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="AlirezaFlh.AudiLogging" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AlirezaFlh.AudiLogging --version 1.0.2                
#r "nuget: AlirezaFlh.AudiLogging, 1.0.2"                
#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.
// Install AlirezaFlh.AudiLogging as a Cake Addin
#addin nuget:?package=AlirezaFlh.AudiLogging&version=1.0.2

// Install AlirezaFlh.AudiLogging as a Cake Tool
#tool nuget:?package=AlirezaFlh.AudiLogging&version=1.0.2                

AuditLog Package

The AuditLog Package is a flexible and extensible solution for implementing audit logging in your .NET applications. It tracks changes to your entities and records them in an audit log, with support for customization and integration into any project.


Features

  • Entity-Agnostic Logging: Works with any IAuditLogEntity implementation.
  • Flexible Configuration: Customize the AuditLog entity, table names, and fields.
  • Seamless Integration: Easily integrate with DbContext and ChangeTracker.
  • Extensible: Optional hooks to modify behavior and configurations.
  • Multi-Project Support: Shareable across projects via NuGet or direct reference.

Installation

Add the package to your project:

dotnet add package AuditLog

Getting Started

1. Register the Services

In your Startup.cs or Program.cs, add the AuditLog services:

services.AddAuditLog(options =>
{
    // Optional: Customize the AuditLog entity configuration
});

2. Configure the DbContext

Update your main DbContext to include audit logging functionality:

public class MyAppDbContext : DbContext
{
    private readonly IAuditLogger _auditLogger;

    public MyAppDbContext(DbContextOptions<MyAppDbContext> options, IAuditLogger auditLogger)
        : base(options)
    {
        _auditLogger = auditLogger;
    }

    public DbSet<MyEntity> MyEntities { get; set; }

    public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
    {
        // Log changes to the database
        await _auditLogger.LogChangesAsync(ChangeTracker);

        // Save the actual changes
        return await base.SaveChangesAsync(cancellationToken);
    }
}

3. Default AuditLog Entity

The package includes a default AuditLog entity. If you need customization, implement your own version of IAuditLogEntity.

Default Entity:
public class AuditLog : IAuditLogEntity
{
    public int Id { get; set; }
    public string TableName { get; set; }
    public int RecordId { get; set; }
    public string Action { get; set; }
    public int? ChangedById { get; set; }
    public DateTime ChangedOn { get; set; } = DateTime.UtcNow;
    public string? OldValues { get; set; }
    public string? NewValues { get; set; }
    public string? IpAddress { get; set; }
}

4. Customizing the Entity

To use a custom entity, implement IAuditLogEntity:

public class CustomAuditLog : IAuditLogEntity
{
    public int Id { get; set; }
    public string TableName { get; set; }
    public int RecordId { get; set; }
    public string Action { get; set; }
    public int? ChangedById { get; set; }
    public DateTime ChangedOn { get; set; } = DateTime.UtcNow;
    public string? OldValues { get; set; }
    public string? NewValues { get; set; }
    public string? IpAddress { get; set; }

    // Custom fields
    public string CustomProperty { get; set; }
    public bool IsCriticalChange { get; set; }
}

Custom Audit Logger Implementation

If you need advanced logging behavior, create a custom IAuditLogger implementation:

public class CustomAuditLogger : IAuditLogger
{
    public Task LogChangesAsync(ChangeTracker changeTracker)
    {
        // Custom logging logic here
    }
}

Register your custom logger in the DI container:

services.AddScoped<IAuditLogger, CustomAuditLogger>();

Usage Scenarios

  • Track changes to critical entities (e.g., user profiles, financial records).
  • Maintain a history of database modifications for compliance.
  • Integrate with multi-project solutions to centralize audit logging.

Example Use Case

var auditLog = new AuditLog
{
    TableName = "Users",
    RecordId = 123,
    Action = "Update",
    ChangedById = 1,
    ChangedOn = DateTime.UtcNow,
    OldValues = "{"Name": "John"}",
    NewValues = "{"Name": "John Doe"}",
    IpAddress = "127.0.0.1"
};

await _auditLogger.LogAsync(new List<IAuditLogEntity> { auditLog });

Contributing

Feel free to open issues or contribute to the package by submitting pull requests.


License

This package is licensed under the MIT License.


Author

Built with ❤️ by [Alireza Fallah]

For questions or feedback, contact us at [alirezaflh70@gmail.com]

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. 
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.3 0 11/25/2024
1.0.2 0 11/25/2024
1.0.1 41 11/21/2024
1.0.0 40 11/21/2024