Audit Trail Component , .net standard 2.1 + Full Source Code

Description
Audit Trail Component + Full Source Code
Version v1.0
Introduction
Audit trail component stores database actions (Add, Update, Delete), business actions (you can specify them), sends notifications and logs its own errors, also it’s dynamic as it gives user the ability to specify , , [Notification’s subject] and [Notification’s Message], also you can search for all the data has been stored by the component by predefined methods.
Framework: .net 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 business actions.
- Send notifications.
- Log send notification process, and retry if it’s failed.
- Log errors.
- Dynamic: User can specify actions and emails and specify if the component should send notification when a specific action happened.
Recommendation :
use background job to execute component methods, to ensure that the original request time will not be increased.
Database Tables
Audit.Action
to store actions lookup data
Audit.ActionUserGroup
to make relation between , and emails, so if this action happened then the component will send notification to the related emails with the specified message.
Audit.AuditTrail
to store the audit data
Audit.ErrorLog
to log the component errors
Audit.Notification
to store the notifications that the component will send to emails
Audit.NotificationLog
to log the send notification process, component will store every (send email) retry and store if it’s done successfully or not with error message
How to Register
- In your project, you need to install Hangfire nugetpackages [Hangfire.AspNetCore, Hangfire.SqlServer] ,or any package to perform background processing.
- Also you need to implement[ IEmailService ] interface to send notification emails .
Startup class
- In method
#region hangfire
// Add Hangfire services.
services.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
services.AddHangfireServer();
#endregion
#region Audit
services.AddScoped<IEmailService, EmailService>();
//AddAuditTrail classes
services.AddAuditTrail(Configuration.GetConnectionString(“DefaultConnection”),
new List<AuditActionDto> {
new AuditActionDto() { ActionCode =”CreateOrder”,SendNotification=true,NotificationFromEmail=”admin@gmail.com“
,NotificationEmails=”ahmed@yahoo.com,ali@hotmail.com”,NotificationSubject=”subject “
,NotificationMessage=”sample message”},
new AuditActionDto() { ActionCode = “DeleteOrder”,SendNotification=true,NotificationFromEmail=”admin@gmail.com”,
NotificationEmails=”ahmed2@yahoo.com,ali2@hotmail.com”,NotificationSubject=”subject “ ,
NotificationMessage=”sample message2” },
new AuditActionDto() { ActionCode = “UpdateOrder” ,SendNotification=true},
new AuditActionDto() { ActionCode = “NewUpdateOrder” ,SendNotification=false}});
//register background job to Check Notifications to send emails
var sp = services.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 and in the list .. select project
- Write the below commands
- add-migration InitiatAudit -context AuditDbContext
- update-database -context AuditDbContext
How to use
- Inject this interface to your class
- To store business action
BackgroundJob.Enqueue(() => AuditTrailService.SaveCustomActionsAuditTrailAsync(“DeleteOrder”, “sample data by hangfire”, “username1”));
- To store database action ,use the below code before Entity framework save changes method
BackgroundJob.Enqueue(() =>AuditTrailService.SaveDbActionsAuditTrailAsync(ApplicationDbContext.ChangeTracker.Entries()
.Select(a =>
new DatabaseChangesDto
{
Entity = a.Entity,
OriginalValues = a.OriginalValues.ToObject(),
CurrentValues = a.CurrentValues.ToObject()
}).ToList()));
- To search for audit data
AuditTrailFilterDto model = new AuditTrailFilterDto {Audit_ActionCode=”DeleteOrder” };
var result=await AuditTrailService.Search(model);
- To search for Notifications
NotificationFilterDto model = new NotificationFilterDto { Audit_ActionCode=”DeleteOrder” };
var result=await NotificationPublicService.SearchNotifications(model);
- to search for notification log
NotificationLogFilterDto model = new NotificationLogFilterDto { };
var result = await NotificationService.SearchNotificationLogs(model);
- to search for error logs
ErrorLogFilterDto model4 = new ErrorLogFilterDto();
var result = await ErrorLogPublicService.Search(model4);
| Type | .NET Script |
|---|---|
| Author | afmelwekeel |
| Category | Net |
| Price | $10 |
| Sales | 6 |
| Published | May 10, 2020 |
| Last Update | May 10, 2020 |
| Compatible Browsers | IE6, IE7, IE8, IE9, IE10, IE11, Firefox, Safari, Opera, Chrome, Edge |
| Compatible Software | Other |
| Source Files Included | C# CS, JavaScript JS, HTML, CSS |
External listing. code.market does not sell this product and does not process payment for it. Pricing, payment, delivery, licensing, refunds, and support are handled by the third-party merchant on its own website. We may earn an affiliate commission from a referral. Details.
Products of the Week.
Submit🥇
Free LinkedIn Likes From Real Members | LinkedInLikes
Get free LinkedIn likes on your posts from real members who react by hand. No password needed: you only share a link.
Website · 12,847 views · 46 clicksWebsite · Week 49 · 12,847 views · 46 clicks
🥈
Free Medium Claps From Real Writers | MediumPlace
Free Medium claps on your stories from verified accounts of real writers, each given by hand. No bots, no Medium password, nothing to pay. Paste a link.
Website · 7,964 views · 10 clicksWebsite · Week 49 · 7,964 views · 10 clicks
🥉
Free GitHub Stars From Real Developers | GitWow
Get free GitHub stars on your repository from real developers who star by hand. No token or password, and it's free.
Website · 15,624 views · 28 clicksWebsite · Week 49 · 15,624 views · 28 clicks
#4
Free Substack Likes From Real Writers | StackRooms
Free Substack likes on your posts and Notes from verified accounts of real writers, given by hand. Free to ask, free to receive. Just share a link.
Website · 7,784 views · 2 clicksWebsite · Week 49 · 7,784 views · 2 clicks
#5
Free Twitter Likes From Real People | SupaTwitter
Free Twitter likes on your tweets from verified accounts of real people, each given by hand. No bots, no Twitter password, nothing to pay. Paste a link.
Website · 7,679 views · 4 clicksWebsite · Week 49 · 7,679 views · 4 clicks
#6
Free X Likes From Real People, Given by Hand | XFast
Free X likes on your posts from verified accounts of real people, each one given by hand. No bots, no X password, no conditions. Paste a link to start.
Website · 7,591 views · 0 clicksWebsite · Week 49 · 7,591 views · 0 clicks