Raven DB provider for Audit.NET library (An extensible framework to audit executing operations in .NET).
Store the audit events in Raven DB collections using the RavenDB.Client library.
NuGet Package To install the package run the following command on the Package Manager Console:
PM> Install-Package Audit.NET.RavenDB
Please see the Audit.NET Readme
Set the static Audit.Core.Configuration.DataProvider
property to set the Raven DB data provider, or call the UseRavenDB
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 RavenDbDataProvider(config => config
.WithSettings("http://127.0.0.1:8080", "AuditEvents"));
Or using the constructor overload that accepts a fluent API:
Audit.Core.Configuration.DataProvider = new RavenDbDataProvider(config => config
.WithSettings(settings => settings
.Urls("http://127.0.0.1:8080")
.DatabaseDefault("AuditEvents")));
Or by using the global setup extension UseRavenDB()
that also accepts the fluent API:
Audit.Core.Configuration.Setup()
.JsonNewtonsoftAdapter()
.UseRavenDB(config => config
.WithSettings(settings => settings
.Urls("http://127.0.0.1:8080")
.Database(ev => "Audit_" + ev.EventType)
.Certificate(cert)));
You can provide the database name setting as function of the Audit Event.
You can also provide an instance of a IDocumentStore
already configured. For example:
Audit.Core.Configuration.Setup()
.UseRavenDB(config => config
.UseDocumentStore(new DocumentStore()
{
Urls = new[] {"http://127.0.0.1:8080"},
Database = "AuditEvents",
Conventions = new DocumentConventions()
{
Serialization = new NewtonsoftJsonSerializationConventions()
{
JsonContractResolver = new AuditContractResolver(),
CustomizeJsonSerializer = ser =>
{
ser.DefaultValueHandling = DefaultValueHandling.Ignore;
}
}
}
}));
IMPORTANT NOTES:
-
The Raven DB C# Client depends on
Newtonsoft.Json
for serialization and deserialization, so if you are targeting .NET 5.0 or higher, it is highly recommended to set up the global Newtonsoft JsonAdapter by adding the.JsonNewtonsoftAdapter()
call on your startup. For example:Audit.Core.Configuration.Setup() .JsonNewtonsoftAdapter() .UseRavenDB(...);
-
This data provider uses a specialized Contract Resolver (
AuditContractResolver
) that honors the decorations fromSystem.Text.Json
but still usingNewtonsfot.Json
as required by the Raven DB client. ThisIContractResolver
is added by default to the client conventions when using.WithSettings(...)
fluent API.
- Urls: The RavenDB server URLs for the initial connection.
- DatabaseDefault: The name of the default database to use.
- Certificate: The certificate for secure connection.
- DatabaseFunc: A function of the
AuditEvent
that returns the name of the database to store the audit event.
- DocumentStore: Gets ot sets the instance of the document store being used.
The provider options can be set with a fluent API described by the following methods:
- WithSettings(): Use the given RavenDB settings.
- UseDocumentStore(): Alternative to use the given RavenDB document store instance (
IDocumentStore
).
This provider implements GetEvent
and GetEventAsync
methods to obtain an audit event by id:
var event = ravenDataProvider.GetEvent("AuditEvents/3201-A");
This provider has the following constraints:
- The table to store the audit events must exists on RavenDB.
To create a database from a Raven DB Data Provider instance you can use the DocumentStore
property, for example:
ravenProvider.DocumentStore.Maintenance.Server.Send(new CreateDatabaseOperation(new DatabaseRecord("AuditEvents")));