Is there any way to implement Audit Trails for user actions in .Net MVC
NickName:Katta Omkareshwar Ask DateTime:2020-06-30T17:09:10

Is there any way to implement Audit Trails for user actions in .Net MVC

I'm new to .Net MVC. I've designed a web application using Ado.Net CRUD operation without entity framework. I've searched on the internet on how to implement audit trails on .Net MVC application, but I'm getting results on the implementation of Audit trails on EF application. Can someone provide any resource reference or example? Thank you guys in advance!

Copyright Notice:Content Author:「Katta Omkareshwar」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/62654001/is-there-any-way-to-implement-audit-trails-for-user-actions-in-net-mvc

Answers
Catalin 2021-01-08T08:57:26

\nI need to capture the user visited page and action performed on that page\n\nYou can implement an HttpModule which logs all the visited pages within your .NET MVC application.\nWith an HttpModule you have access to all the Request/Response properties, including Headers, Cookies, FormData parameters.\nnamespace MyApplication\n{\n public class DatabaseAuditHttpModule : IHttpModule\n {\n private class Properties\n {\n public string Url { get; set; }\n public string HttpMethod { get; set; }\n public int StatusCode { get; set; }\n \n // add other important HTTP properties\n }\n \n public void Init(HttpApplication context)\n {\n context.BeginRequest += BeginRequest;\n context.EndRequest += EndRequest;\n }\n\n private void BeginRequest(object sender, EventArgs e)\n {\n HttpContext ctx = HttpContext.Current;\n var request = ctx.Request;\n\n var requestHeaders = request.Unvalidated.Headers;\n var requestFormData = request.Unvalidated.Form;\n\n var properties = new Properties\n {\n Url = request.Url.ToString(),\n HttpMethod = request.HttpMethod\n };\n\n ctx.Items["X-Properties"] = properties;\n }\n\n\n private void EndRequest(object sender, EventArgs e)\n {\n HttpContext ctx = HttpContext.Current;\n\n // get the properties for the current HTTP request\n Properties properties = (Properties)HttpContext.Current.Items["X-Properties"];\n\n properties.StatusCode = ctx.Response.StatusCode;\n \n // TODO:\n // log these values in the database\n }\n\n public void Dispose()\n {\n\n }\n }\n}\n\nRegistering the HttpModule in Global.asax:\nnamespace MyApp.Mvc\n{\n public class MvcApplication : System.Web.HttpApplication\n {\n protected void Application_Start()\n {\n \n }\n \n // register DatabaseAuditHttpModule\n public static DatabaseAuditHttpModule AuditHttpModule = new DatabaseAuditHttpModule();\n \n public override void Init()\n {\n base.Init();\n \n AuditHttpModule.Init(this);\n }\n }\n}\n\nLogging stored procedures execution example:\npublic class DatabaseService\n{\n public static async Task ExecuteStoredProcedureAsync(string connectionString, string storedProcedureName, ILogger logger)\n {\n logger.LogTrace($"'{storedProcedureName}' Stored Procedure executing");\n long totalDuration = 0;\n\n Stopwatch sw = new Stopwatch();\n sw.Start();\n\n using(var connection = new SqlConnection(connectionString))\n {\n connection.Open();\n \n sw.Stop();\n totalDuration += sw.ElapsedMilliseconds;\n\n logger.LogTrace($"connection.Open() Duration:{sw.ElapsedMilliseconds}");\n\n sw.Restart();\n \n using (var command = SqlCommand(storedProcedureName, connection))\n {\n command.CommandType = CommandType.StoredProcedure;\n\n using (var reader = await command.ExecuteReaderAsync())\n {\n sw.Stop();\n totalDuration += sw.ElapsedMilliseconds;\n\n logger.LogTrace($"'{storedProcedureName}' Stored Procedure ExecuteReader Duration:{sw.ElapsedMilliseconds}");\n\n sw.Restart();\n\n do\n {\n while (await reader.ReadAsync())\n {\n // read the data\n }\n\n } while (await reader.NextResultAsync());\n }\n\n sw.Stop();\n totalDuration += sw.ElapsedMilliseconds;\n\n logger.LogTrace($"'{storedProcedureName}' Stored Procedure executed. Duration:{totalDuration}");\n\n return result;\n }\n }\n }\n}\n",


More about “Is there any way to implement Audit Trails for user actions in .Net MVC” related questions

Is there any way to implement Audit Trails for user actions in .Net MVC

I'm new to .Net MVC. I've designed a web application using Ado.Net CRUD operation without entity framework. I've searched on the internet on how to implement audit trails on .Net MVC application, b...

Show Detail

Using Oracle Streams to implement audit trails

I'm going to implement asynchronous audit trails functionality for highly loaded system with using of Oracle Streams (for log mining on redo and archive logs). Audit trails in my case mustn't slow ...

Show Detail

Oracle clean audit trails

I am trying to clean the unified audit trails using the plsql below DBMS_AUDIT_MGMT.clean_audit_trail( audit_trail_type => DBMS_AUDIT_MGMT.AUDIT_TRAIL_UNIFIED, use_last_arch_timestamp => TRUE...

Show Detail

Oracle clean audit trails

I am trying to clean the unified audit trails using the plsql below DBMS_AUDIT_MGMT.clean_audit_trail( audit_trail_type => DBMS_AUDIT_MGMT.AUDIT_TRAIL_UNIFIED, use_last_arch_timestamp => TRUE...

Show Detail

What is the clean way to Implement Audit Trail in Asp.net MVC and Web API

I am trying to look for a more clean way to add audit trail function to an exist asp.net MVC and Web Api project which contains hundreds of Controller and ApiController. The Audit trail log would ...

Show Detail

Turning off audit trails of AWS CloudTrail

AWS CloudTrail Events has a feature which turns on audit trails automatically in AWS account and keeps the data for couple of days. Due to some compliance, want this feature to be turned off. Is t...

Show Detail

Audit Trails for Adding/Modifying Fields, Tables, Layouts, Scripts in Filemaker 13

Is there a way to implement audit trails eg. by scripting or 3rd-party plugin that track Adding Fields, Modifying Field Types, Adding Tables, Adding Layouts etc. in Filemaker 13, whichever the vers...

Show Detail

ORM with built-in audit trail

I am designing an ASP.NET MVC web app with a database backend that requires a full audit trail for regulatory purposes. I've implemented audit trails in the past but it feels like I would be safer...

Show Detail

Allowing user to rollback from db audit trail with SQLAlchemy

I'm starting to use SQLAlchemy for a new project where I was planning to implement an audit trail similar to the one proposed on these questions: Implementing Audit Trail for Objects in C#? Audit ...

Show Detail

PyDAL audit trails outside of web app?

I am wondering if pydal supports audit trails when you are not using the webapp. My investigations [1][2] show that an auth module is needed for audit trails, but that appears to be something relat...

Show Detail