Store Audit.NET Trail Logs into a MySQL database
$ dotnet add package Audit.NET.MySqlMySQL provider for Audit.NET library (An extensible framework to audit executing operations in .NET).
Store the audit events in a MySQL Table.
NuGet Package To install the package run the following command on the Package Manager Console:
PM> Install-Package Audit.NET.MySql
Please see the Audit.NET Readme
Set the static Audit.Core.Configuration.DataProvider property to set the MySQL data provider, or call the UseMySql method on the fluent configuration. This should be done before any AuditScope creation, i.e. during application startup.
For example:
Audit.Core.Configuration.DataProvider = new MySqlDataProvider()
{
ConnectionString = "Server=localhost; Database=events; Uid=admin; Pwd=admin;",
TableName = "events",
IdColumnName = "event_id",
JsonColumnName = "data",
CustomColumns = new List<CustomColumn>()
{
new CustomColumn("user", ev => ev.Environment.UserName)
}
};
Or by using the fluent configuration API:
Audit.Core.Configuration.Setup()
.UseMySql(config => config
.ConnectionString("Server=localhost; Database=events; Uid=admin; Pwd=admin;")
.TableName("events")
.IdColumnName("event_id")
.JsonColumnName("data")
.CustomColumn("user", ev => ev.Environment.UserName));
This provider implements GetEvent and GetEventAsync methods to obtain an audit event by id
var event = mySqlDataProvider.GetEvent(1000);
For example:
CREATE TABLE event
(
id INT unsigned NOT NULL AUTO_INCREMENT,
inserted_date DATETIME DEFAULT CURRENT_TIMESTAMP,
last_updated_date DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
data JSON NOT NULL,
user VARCHAR(20) NULL,
PRIMARY KEY (id)
);
Entity Framework Extensions and Dapper Plus are major sponsors and are proud to contribute to the development of Audit.NET
Combine the power of auditing with the speed of Bulk Operations to get the best of both worlds — audit and performance.