erossini / AuditNetWebApi

Implementation of Audit.NET with ASP.NET Core3.1 and Entity Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implementation of an audit strategy with C# and Audit.NET

How to implement an efficient audit system with C# and Entity Framework Core?

Structure of the audit tables

Audit in action

I have talked about Entity Framework in my previous posts:

Often, during my implementation, there is a common request: track all changes in the database and where and who is changing a record.

A way to implement an audit is to override the SubmitChanges function in your data context. I created a post about it a long time ago; it was 2014. The post has title Log record changes in SQL server in an audit table and I was using Entity Framework.

So, I googled a bit and I found a very nice NuGet package that can help us to implement what we need. Audit.NET is the package and it is pretty complete. Although the documentation is good, I was struggle to understand how to use it in my project. For this reason, I want to give you all my thoughts and the final solution. So, you can use it.

For the full explanation of this code, read Audit with Entity Framework Core

API samples

Description Command
Get all records curl -X GET http://localhost:50732/api/values -H "content-type: application/json"
Get record curl -X GET http://localhost:50732/api/values/1 -H "content-type: application/json"
Insert record curl -X POST http://localhost:50732/api/values -H "content-type: application/json" -d '"Some description"'
Update record curl -X PUT http://localhost:50732/api/values/1 -H "content-type: application/json" -d '"New description"'
Delete record curl -X DELETE http://localhost:50732/api/values/1 -H "content-type: application/json"
Delete multiple records curl -X DELETE http://localhost:50732/api/values/delete -H "content-type: application/json" -d '"2,3,4"'
Get all records curl -X GET http://localhost:50732/api/contacts -H "content-type: application/json"
Get record curl -X GET http://localhost:50732/api/contacts/1 -H "content-type: application/json"
Insert record curl -X POST http://localhost:50732/api/contacts -H "content-type: application/json" -d '"Some description"'
Update record curl -X PUT http://localhost:50732/api/contacts/1 -H "content-type: application/json" -d '"New description"'
Delete record curl -X DELETE http://localhost:50732/api/contacts/1 -H "content-type: application/json"
Delete multiple records curl -X DELETE http://localhost:50732/api/contacts/delete -H "content-type: application/json" -d '"2,3,4"'

If you have any question, please use my forum and visit my blog.

About

Implementation of Audit.NET with ASP.NET Core3.1 and Entity Framework


Languages

Language:C# 100.0%