Store Audit.NET Trail Logs into a PostgreSQL database
$ dotnet add package Audit.NET.PostgreSqlPostgreSQL Server provider for Audit.NET library (An extensible framework to audit executing operations in .NET).
Store the audit events in a PostgreSQL Table, in JSON format.
NuGet Package To install the package run the following command on the Package Manager Console:
PM> Install-Package Audit.NET.PostgreSql
Please see the Audit.NET Readme
Set the static Audit.Core.Configuration.DataProvider property to set the PostgreSQL data provider, or call the UsePostgreSql method on the fluent configuration. This should be done before any AuditScope creation, i.e. during application startup.
For example, using the fluent API:
Audit.Core.Configuration.Setup()
.UsePostgreSql(config => config
.ConnectionString("Server=127.0.0.1;Port=5432;User Id=admin;Password=admin;Database=postgres;")
.TableName("event")
.IdColumnName("id")
.DataJsonColumn("data", DataType.JSONB)
.LastUpdatedColumnName("updated_date")
.CustomColumn("event_type", ev => ev.EventType)
.CustomColumn("user", ev => ev.Environment.UserName));
Or by setting the DataProvider properties directly:
Audit.Core.Configuration.DataProvider = new PostgreSqlDataProvider()
{
ConnectionString = "Server=127.0.0.1;Port=5432;User Id=admin;Password=admin;Database=postgres;",
TableName = "event",
IdColumnName = "id",
DataColumnName = "data",
DataType = "JSONB",
LastUpdatedDateColumnName = "updated_date",
CustomColumns = new List<CustomColumn>()
{
new CustomColumn("event_type", ev => ev.EventType),
new CustomColumn("user", ev => ev.Environment.UserName),
}
};
Mandatory:
Server=127.0.0.1;Port=5432;User Id=postgres;Password=admin;Database=postgres;event.id.Optional:
GetEvents() and EnumerateEvents() will return empty results.STRING.For example:
CREATE TABLE public.event
(
id bigserial NOT NULL,
inserted_date timestamp without time zone NOT NULL DEFAULT now(),
updated_date timestamp without time zone NOT NULL DEFAULT now(),
data jsonb,
event_type varchar(50),
user varchar(50),
CONSTRAINT event_pkey PRIMARY KEY (id)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
This provider implements GetEvent and GetEventAsync methods to obtain an audit event by id.
var event = dataProvider.GetEvent(1000);
The Postgre SQL data provider also includes basic support for querying the events collection.
Use the EnumerateEvents() method on PostgreSqlDataProvider class to run SQL-like queries against the audit events. The EnumerateEvents() method accepts any valid Postgre WHERE clause as a parameter.
For example:
IEnumerable<AuditEvent> events = postgreDataProvider.EnumerateEvents(
"data #> '{Environment,UserName}' = '\"John\"'");
Will return the events whose property Environment.UserName is equal to 'John'.
Note
The methods
GetEvents()andEnumerateEvents()are only available when theDataJsonColumnNameproperty is set.
For complex querying capabilities, you should use the npgsql driver or the Npgsql EntityFramework provider directly.
You can obtain a reference to the underlying NpgsqlConnection used by the data provider by calling the GetDbConnection() method.
For example:
var npgsqlConnection = Audit.Core.Configuration.DataProviderAs<PostgreSqlDataProvider>().GetDbConnection(auditEvent);
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.