Store Audit.NET Trail Logs into Elasticsearch database
$ dotnet add package Audit.NET.ElasticsearchElasticsearch provider for Audit.NET library (An extensible framework to audit executing operations in .NET).
Store the audit events in Elasticsearch database using the NEST library.
NuGet Package To install the package run the following command on the Package Manager Console:
PM> Install-Package Audit.NET.Elasticsearch
Please see the Audit.NET Readme
Set the static Audit.Core.Configuration.DataProvider property to set the Elasticsearch data provider, or call the UseElasticsearch
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 ElasticsearchDataProvider()
{
ConnectionSettings = new AuditConnectionSettings(new Uri("http://localhost:9200")),
IndexBuilder = ev => ev.EventType,
IdBuilder = ev => Guid.NewGuid()
};
Or by using the fluent configuration API:
Audit.Core.Configuration.Setup()
.UseElasticsearch(config => config
.ConnectionSettings(new Uri("http://localhost:9200"))
.Index(auditEvent => auditEvent.EventType)
.Id(ev => Guid.NewGuid()));
Note that you can provide the settings as a function of the Audit Event.
Mandatory:
AuditConnectionSettings.Optional:
This provider implements GetEvent and GetEventAsync methods to obtain an audit event by id:
var event = elasDataProvider.GetEvent(new ElasticsearchAuditEventId() { Index = "myindex", Id = "myid" });