Audit Trail Component , .internet standard 2.1 + Full Source Code
Audit Trail Component + Full Source Code
Version v1.0
Introduction
Audit path element shops database actions (Add, Update, Delete), enterprise actions (you’ll be able to specify them), sends notifications and logs its personal errors, additionally it’s dynamic because it offers person the flexibility to specify [Business actions], [Emails that notifications will be sent to], [Notification’s subject] and [Notification’s Message], additionally you’ll be able to seek for all the information has been saved by the element by predefined strategies.
Framework: .internet standard 2.1
Packages :
- Microsoft.EntityFrameworkCore.SqlServer 3.1.3
- Newtonsoft.Json 12.0.3
Hangfire Packages :
- Hangfire.AspNetCore
- Hangfire.SqlServer
Features :
- Store database actions and enterprise actions.
- Send notifications.
- Log ship notification course of, and retry if it’s failed.
- Log errors.
- Dynamic: User can specify actions and emails and specify if the element ought to ship notification when a selected motion occurred.
Recommendation :
use background job to execute element strategies, to make sure that the unique request time won’t be elevated.
Database Tables
Audit.Action
to retailer actions lookup knowledge
Audit.ActionUserGroup
to make relation between [action], and emails, so if this motion occurred then the element will ship notification to the associated emails with the required message.
Audit.AuditTrail
to retailer the audit knowledge
Audit.ErrorLog
to log the element errors
Audit.Notification
to retailer the notifications that the element will ship to emails
Audit.NotificationLog
to log the ship notification course of, element will retailer each (ship e-mail) retry and retailer if it’s performed efficiently or not with error message
How to Register
- In your venture, it’s essential set up Hangfire nugetpackages [Hangfire.AspNetCore, Hangfire.SqlServer] ,or any bundle to carry out background processing.
- Also it’s essential implement[ IEmailService ] interface to ship notification emails .
Startup class
- In technique [ConfigureServices]
#area hangfire
// Add Hangfire companies.
companies.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(Configuration.GetConnectionString(“DefaultConnection”), new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
UsePageLocksOnDequeue = true,
DisableGlobalLocks = true
}));
��
// Add the processing server as IHostedService
companies.AddHangfireServer();
#endregion
#area Audit
companies.AddScoped<IEmailService, EmailService>();
//AddAuditTrail lessons
companies.AddAuditTrail(Configuration.GetConnectionString(“DefaultConnection”),
new List<AuditActionDto> {
new AuditActionDto() { ActionCode =”CreateOrder”,SendNotification=true,NotificationFromEmail=”admin@gmail.com“
,NotificationEmails=”ahmed@yahoo.com,ali@hotmail.com”,NotificationTopic=”topic “
,NotificationMessage=”pattern message”},
new AuditActionDto() { ActionCode = “DeleteOrder”,SendNotification=true,NotificationFromEmail=”admin@gmail.com”,
NotificationEmails=”ahmed2@yahoo.com,ali2@hotmail.com”,NotificationTopic=”topic “ ,
NotificationMessage=”pattern message2” },
new AuditActionDto() { ActionCode = “UpdateOrder” ,SendNotification=true},
new AuditActionDto() { ActionCode = “NewUpdateOrder” ,SendNotification=false}});
//register background job to Check Notifications to ship emails
var sp = companies.BuildServiceProvider().CreateScope().ServiceProvider;
var notificationService = sp.GetService<INotificationPublicService>();
JobStorage.Current = new SqlServerStorage(Configuration.GetConnectionString(“DefaultConnection”),
new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
UsePageLocksOnDequeue = true,
DisableGlobalLocks = true
});
RecurringJob.AddOrUpdate(() => notificationService.CheckNotificationsAndSend(), Cron.Daily);
#endregion
How to create tables
- Open [Package manager console] and within the [Default project] record .. choose [AuditTrailComponent] venture
- Write the beneath instructions
- add-migration InitiatAudit -context AuditDbContext
- update-database -context AuditDbContext
How to make use of
- Inject this interface to your class [IAuditTrailService]
- To retailer enterprise motion
BackgroundJob.Enqueue(() => AuditTrailService.SaveCustomActionsAuditTrailAsync(“DeleteOrder”, “pattern knowledge by hangfire”, “username1”));
- To retailer database motion ,use the beneath code earlier than Entity framework save modifications technique
BackgroundJob.Enqueue(() =>AuditTrailService.SaveDbActionsAuditTrailAsync(ApplicationDbContext.ChangeTracker.Entries()
.Select(a =>
new DatabaseChangesDto
{
Entity = a.Entity,
OriginalValues = a.OriginalValues.ToObject(),
CurrentValues = a.CurrentValues.ToObject()
}).ToList()));
- To seek for audit knowledge
//Inject this interface [IAuditTrailService]
AuditTrailFilterDto mannequin = new AuditTrailFilterDto {Audit_ActionCode=”DeleteOrder” };
var outcome=await AuditTrailService.Search(mannequin);
- To seek for Notifications
//Inject this interface [INotificationPublicService]
NotificationFilterDto mannequin = new NotificationFilterDto { Audit_ActionCode=”DeleteOrder” };
var outcome=await NotificationPublicService.SearchNotifications(mannequin);
- to seek for notification log
//Inject this interface [IErrorLogPublicService]
NotificationLogFilterDto mannequin = new NotificationLogFilterDto { };
var outcome = await NotificationService.SearchNotificationLogs(mannequin);
- to seek for error logs
//Inject this interface [IErrorLogPublicService]
ErrorLogFilterDto model4 = new ErrorLogFilterDto();
var outcome = await ErrorLogPublicService.Search(model4);