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🥇
STOP GUESSING WHAT TO POST
AI that learns your brand voice and builds a month of consistent, on-brand social content: carousels, posts and hooks that sound like you, not like everyone else's AI.
SaaS · 4,310 views · 38 clicksSaaS · Week 45 · 4,310 views · 38 clicks
🥈
Conversational Backlinks for Busy Founders
Place Backlink Relevant Conversation for your domain across Forums and Conversations
Website · 42,043 views · 223 clicksWebsite · Week 45 · 42,043 views · 223 clicks
🥉
upvote.club
Free Social Media Engagement that Helps Boost Your Content. Upvote Club supports 19 social networks with 100+ engagement actions in one dashboard. Add links to your posts and get free engagement from real users.
SaaS · 40,088 views · 117 clicksSaaS · Week 45 · 40,088 views · 117 clicks
#4
Substack Posting API
Onboard your Agent to Substack. Write Publish on Substack via API
SaaS · 59,094 views · 198 clicksSaaS · Week 45 · 59,094 views · 198 clicks
#5
Seenshot App
Free, Fast, MacOS Screenshot app. Make Screenshot cmd+shift+2 and send it via link
Desktop App · 28,467 views · 122 clicksDesktop App · Week 45 · 28,467 views · 122 clicks
#6
Hand-Placed Backlinks $9.99 per link. Flat rate.
Buy hand-placed forum, Q-A and directory backlinks. Flat $9.99 per link, no subscriptions — you only pay for approved listings.
Website · 4,299 views · 19 clicksWebsite · Week 45 · 4,299 views · 19 clicks
#7
Free Invoice Generator - PDF Invoices in Your Browser, No Sign-Up
A free invoice maker for freelancers and small businesses: fill in the invoice, check the live preview, and download a PDF that looks exactly like it. Any currency, tax per item, three templates, no sign-up, and nothing leaves your device.
2,976 views · 17 clicksWeek 45 · 2,976 views · 17 clicks
#8
Merge PDF Files Free - Combine PDFs in Your Browser, No Upload
A free PDF merger for office workers, students, and freelancers: combine PDFs and images into one file in the browser, reorder files and pages, pick page ranges, keep bookmarks, and open password-protected PDFs. Nothing is uploaded, no sign-up, no watermark.
2,747 views · 13 clicksWeek 45 · 2,747 views · 13 clicks