Store Audit.NET Trail Logs into a MongoDB database
$ dotnet add package Audit.NET.MongoDBMongo DB provider for Audit.NET library. (An extensible framework to audit executing operations in .NET).
Store the audit events in a Mongo DB Collection, in BSON format.
NuGet Package
PM> Install-Package Audit.NET.MongoDB
Please see the Audit.NET Readme
Set the static Audit.Core.Configuration.DataProvider property to set the Mongo DB data provider, or call the UseMongoDB 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 Audit.MongoDB.Providers.MongoDataProvider()
{
ConnectionString = "mongodb://localhost:27017",
Database = "Audit",
Collection = "Event"
};
Or by using the fluent configuration API:
Audit.Core.Configuration.Setup()
.UseMongoDB(config => config
.ConnectionString("mongodb://localhost:27017")
.Database("Audit")
.Collection("Event"));
Instead of using the connection string, you have the option to supply the MongoClientSettings instead:
Audit.Core.Configuration.Setup()
.UseMongoDB(config => config
.ClientSettings(new MongoClientSettings()
{
Server = new MongoServerAddress("localhost", 27017),
UseTls = true
})
.Database("Audit")
.Collection("Event"));You can find more information about MongoClientSettings here.
Mandatory:
An example of the output as seen with NoSQL Manager for Mongo DB:

This provider implements GetEvent and GetEventAsync methods to obtain an audit event by id:
var event = mongoDataProvider.GetEvent("57b60f25be5914a355771629");The Mongo DB data provider also includes support for querying the events collection.
You can use the QueryEvents() method on MongoDataProvider class to run LINQ queries against the audit events.
For example, to get the top 10 most time-consuming events for a specific machine:
IQueryable<AuditEvent> query = mongoDataProvider.QueryEvents()
.Where(ev => ev.Environment.MachineName == "HP")
.OrderByDescending(ev => ev.Duration)
.Take(10);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.