Redis storage provider for Audit.NET library (An extensible framework to audit executing operations in .NET).
Store the audit events in a Redis database as Strings, List, SortedSet, Hash, publish to a PubSub channel or add to a Redis Stream.
NuGet Package To install the package run the following command on the Package Manager Console:
PM> Install-Package Audit.NET.Redis
Please see the Audit.NET Readme
To set the Redis data provider globally, use the fluent configuration API, for example:
Audit.Core.Configuration.Setup()
.UseRedis(redis => redis
.ConnectionString(RedisCnnString)
.AsString(str => str
.Key(ev => $"{Guid.NewGuid()}")));If you need to set different providers/configuration per scope, you can use the RedisDataProviderHelper to easily get a new Redis Data Provider via the fluent API, for example:
var dp = new RedisDataProviderHelper(RedisCnnString)
.AsString(str => str
.Key(ev => $"{Guid.NewGuid()}"));
using (AuditScope.Create(new AuditScopeOptions() { DataProvider = dp }))
{
// ...
}ConnectionStringindicates the redis connection string, based on StackExchange.Redis configuration.ConfigurationOptionsindicates the redis connection configuration options, when you need to provide custom connection options. Alternative to ConnectionString.Serializerindicates a custom serialization method for the audit events. By default the events are serialized as JSON encoded as UTF-8.
This data provider allows to store the events in different ways.
Store each audit event as a redis string.
Setup sample:
// Store audits as strings.
Audit.Core.Configuration.Setup()
.UseRedis(redis => redis
.ConnectionString("localhost:6379,allowAdmin=true")
.AsString(str => str
.Key(ev => $"{ev.EventType}:{Guid.NewGuid()}")));Mandatory settings
- Use the
Keymethod to indicate the redis key where the event will be stored, this key can depend on theAuditEventobject.
Optional settings
TimeToLivespecifies the Time To Live for the Redis Key. Default is no TTL.Databasespecifies the database ID to use. Default is database 0.AttachTaskattaches an additional redis command to the execution.
Adds each audit event to a Redis Stream.
Setup sample:
// Store audits as strings.
Audit.Core.Configuration.Setup()
.UseRedis(redis => redis
.ConnectionString("localhost")
.AsStream(stream => stream
.Key("MyAuditStream")
.MaxLength(5000)
.WithCustomField("Date", ev => DateTime.UtcNow.ToString())));Mandatory settings
- Use the
Keymethod to indicate the stream redis key, this key can depend on theAuditEventobject.
Optional settings
Databasespecifies the database ID to use. Default is database 0.AttachTaskattaches an additional redis command to the execution.MaxLengthspecifies the maximum quantity of events that the stream will store. Older elements will be deleted. Default is NULL for no-limit.DefaultAuditEventFieldNamespecifies the default field name that will contain the AuditEvent JSON representation in the stream entry. Default is "AuditEvent".WithCustomFieldallows specifying custom fields to be stored in the stream entry. By default, only the field named "AuditEvent" is stored, containing the JSON representation of the Audit Event.
Store each audit event as a field in a redis hash.
Setup sample:
// Store audits in hashes per each EventType.
Audit.Core.Configuration.Setup()
.UseRedis(redis => redis
.ConnectionString(RedisCnnString)
.AsHash(hash => hash
.Key(ev => $"{ev.EventType}")
.HashField(ev => $"{Guid.NewGuid()}")));Mandatory settings:
Keyto indicate the redis key where the hash will be stored, this key can depend on theAuditEventobject.HashFieldto indicate the redis field inside the hash where the event will be stored.
Optional settings
TimeToLivespecifies the Time To Live for the Redis Hash. Default is no TTL.Databasespecifies the database ID to use. Default is database 0.AttachTaskattaches an additional redis command to the execution.
Store each audit event as a member in a redis list.
Setup sample:
// Store audits in lists per each EventType, with a maximum of 1000 events per list.
Audit.Core.Configuration.Setup()
.UseRedis(redis => redis
.ConnectionString(RedisCnnString)
.AsList(list => list
.Key(ev => $"{ev.EventType}")
.MaxLength(1000)));Mandatory settings:
Keyto indicate the redis key where the list will be stored, this key can depend on theAuditEventobject.
Optional settings:
MaxLengthto indicate the maximum quantity of events that the list will store. Older elements will be deleted. Default is no-limit.TimeToLivespecifies the Time To Live for the Redis List. Default is no TTL.Databasespecifies the database ID to use. Default is database 0.AttachTaskattaches an additional redis command to the execution.
Store each audit event as a member in a redis sorted set.
Setup sample:
// Store audits in sorted sets per each EventType, maintaining only the events from the last 30 days.
Audit.Core.Configuration.Setup()
.UseRedis(redis => redis
.ConnectionString(RedisCnnString)
.AsSortedSet(sset => sset
.Key(ev => $"{ev.EventType}")
.Score(ev => ev.StartDate.Ticks)
.MinScore(ev => DateTime.UtcNow.AddDays(-30).Ticks)));Mandatory settings:
Keyto indicate the redis key where the sorted set will be stored, this key can depend on theAuditEventobject.Scoreto indicate the score as adoublethat the event will have, can depend on theAuditEventobject.
Optional settings:
MinScorespecifies a function that returns the minimum score allowed for the sorted set. Audits with score less than the minimum will be deleted. This deletion takes place when a new event is stored.MaxScorespecifies a function that returns the maximum score allowed for the sorted set.MaxRankspecifies a function that returns the maximum rank allowed for the sorted set. Max>0: Maintain only the top N scored elements. Max<0: Maintain only the bottom N scored elements.TimeToLivespecifies the Time To Live for the Redis Sorted Set. Default is no TTL.Databasespecifies the database ID to use. Default is database 0.AttachTaskattaches an additional redis command to the execution.
Sends the audit events to a redis PubSub channel.
Setup sample:
// Send audits to PubSub channels.
Audit.Core.Configuration.Setup()
.UseRedis(redis => redis
.ConnectionString(RedisCnnString)
.AsPubSub(pub => pub
.Channel(ev => $"audits:{ev.EventType}")));Mandatory settings:
Channelto indicate the channel name to use for publishing the events.
This provider implements GetEvent and GetEventAsync methods to obtain an audit event by id:
var event = Configuration.DataProvider.GetEvent("eventId");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.

