Pandatech.AuditTrail is a tool meticulously crafted to gather vital entity data from the change tracker post DbContext SaveChanges operation.
Audits necessary entities for logging or preserving modified property data. Offers a convenient fluent configuration to specify tracked entities with ease.
Install the package via NuGet Package Manager or use the following command:
Install-Package Pandatech.AuditTrail
- Implement
IAuditTrailConsumer
interface. - Add AddAuditTrail services by providing
IAuditTrailConsumer
implementation. - Create a rules derived from
EntityRule
.
Implement the IAuditTrailConsumer
interface.
To be able to retrive modified entities data after save operation.
public class AuditTrailConsumer<TPermission>() : IAuditTrailConsumer<TPermission>
{
public async Task ConsumeAsync(IEnumerable<AuditTrailDataBeforeSave<TPermission>> auditData,
DbContextEventData? eventData,
CancellationToken cancellationToken = default)
{
// Handle tracked entites here.
}
}
builder.Services.AddAuditTrail<PermissionType?, AuditTrailConsumer<PermissionType?>>(typeof(Registration).Assembly);
services.AddDbContextPool<SomeDbContext>((sp, options) =>
{
options.UseAuditTrail<PermissionType?>(sp);
});
Create a rule derived from EntityRule
.
public class UserRule : EntityRule<UserEntity, PermissionType?>
{
public UserRule(ISomeService someService)
{
SetPermission(PermissionType.Users_Read);
RuleFor(s => s.PasswordHash).Ignore();
RuleFor(s => s.Status).ChangeName("UserStatus");
}
}
You can create your own rules and modify properties as needed.
public class ChangeNamePropertyRule<TEntity, TProperty> : PropertyRule<TEntity, TProperty>
{
private readonly string _changeName;
public override NameValue ExecuteRule(string name, object value)
{
return new NameValue(_changeName, value);
}
public ChangeNamePropertyRule(string name)
{
_changeName = name;
}
}
public static class RuleExtensions
{
public static IRuleBulder<T, TPermission, TProperty?> ChangeName<T, TPermission, TProperty>(this IRuleBulder<T, TPermission, TProperty> ruleBuilder, string name)
where T : class
{
var rule = new ChangeNamePropertyRule<T, TProperty?>(name);
return ruleBuilder.SetRule(rule!)!;
}
}
- In case of composite keys only first key will be selected as entityId.
Contributions are welcome! Please submit a pull request or open an issue to propose changes or report bugs.
Pandatech.AuditTrail is licensed under the MIT License.