Audit.NET.SqlServer 31.0.1

Audit.NET.SqlServer

Sql Server provider for Audit.NET library (An extensible framework to audit executing operations in .NET).

Store the audit events in a SQL Table, in JSON format.

Install

NuGet Package To install the package run the following command on the Package Manager Console:

PM> Install-Package Audit.NET.SqlServer

NuGet Status NuGet Count

Usage

Please see the Audit.NET Readme

Configuration

Set the static Audit.Core.Configuration.DataProvider property to set the Sql Server data provider, or call the UseSqlServer 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 SqlDataProvider()
{
    ConnectionString = "data source=localhost;initial catalog=Audit;integrated security=true;",
    Schema = "dbo",
    TableName = "Event",
    IdColumnName = "EventId",
    JsonColumnName = "JsonData",
    LastUpdatedDateColumnName = "LastUpdatedDate",
    CustomColumns = new List<CustomColumn>()
    {
        new CustomColumn("EventType", ev => ev.EventType)
    }
};

Or by using the fluent configuration API:

Audit.Core.Configuration.Setup()
    .UseSqlServer(config => config
        .ConnectionString("data source=localhost;initial catalog=Audit;integrated security=true;")
        .Schema("dbo")
        .TableName("Event")
        .IdColumnName("EventId")
        .JsonColumnName("JsonData")
        .LastUpdatedColumnName("LastUpdatedDate")
        .CustomColumn("EventType", ev => ev.EventType)        
        .CustomColumn("User", ev => ev.Environment.UserName));

You can provide any of the settings as a function of the Audit Event, for example to use a connection string per machine, and different table names:

Audit.Core.Configuration.Setup()
    .UseSqlServer(config => config
        .ConnectionString(ev => GetCnnString(ev.Environment.MachineName))
        .TableName(ev => ev.EventType == "Order" ? "OrderAudits" : "Audits"));

Provider Options

  • ConnectionString: The SQL Server connection string.
  • DbConnection: The DbConnection to use, alternative to ConnectionString.
  • DbContextOptions: To set custom options to use with the default Audit EF DbContext. This setting is ignored if a DbContext instance is provided.
  • DbContext: Specifies a custom EntityFramework DbContext for querying and inserting audit events. When this option is provided, the ConnectionString, DbConnection, and DbContextOptions settings are ignored.
  • Schema: The SQL schema for the table. (optional)
  • TableName: The audit events table name.
  • IdColumnName: The column name of the event identifier (the primary key).
  • JsonColumnName: The column name of the event table where the audit event JSON will be stored. (optional)
  • LastUpdatedDateColumnName: The datetime column name to update when replacing events. (optional)
  • CustomColumn: Additional columns to store information from the audit event. (optional)

Query events

This provider implements GetEvent and GetEventAsync methods to obtain an audit event by id:

var event = sqlDataProvider.GetEvent(1000);

Table constraints

  • The table should exists.
  • The table should have a single ID column (Unique or Primary key).
  • The type of the ID column should be convertible to NVARCHAR.

For example:

CREATE TABLE [Event]
(
    [EventId] BIGINT IDENTITY(1,1) NOT NULL,
    [InsertedDate] DATETIME NOT NULL DEFAULT(GETUTCDATE()),
    [LastUpdatedDate] DATETIME NULL,
    [JsonData] NVARCHAR(MAX) NOT NULL,
    [EventType] NVARCHAR(100) NOT NULL,
    CONSTRAINT PK_Event PRIMARY KEY (EventId)
)
GO

If you use Azure SQL Server or Sql Server 2016, you can create indexes on the JSON fields, for example creating a schemabinded view:

CREATE VIEW dbo.[v_Event] WITH SCHEMABINDING
AS
SELECT EventId, 
    InsertedDate,
    CAST(JSON_VALUE(JsonData, '$.EventType') AS NVARCHAR(255)) AS [EventType],
    CAST(JSON_VALUE(JsonData, '$.ReferenceId') AS NVARCHAR(255)) AS [ReferenceId],
    JSON_VALUE(JsonData, '$.Target.Type') As [TargetType],
    COALESCE(JSON_VALUE(JsonData, '$.Target.Old'), JSON_QUERY(JsonData, '$.Target.Old')) AS [TargetOld],
    COALESCE(JSON_VALUE(JsonData, '$.Target.New'), JSON_QUERY(JsonData, '$.Target.New')) AS [TargetNew],
    JSON_QUERY(JsonData, '$.Comments') AS [Comments],
    [JsonData]
FROM dbo.[Event]
GO

CREATE UNIQUE CLUSTERED INDEX PK_V_EVENT ON [v_Event] (EventId)
GO
CREATE INDEX IX_V_EVENT_EventType_ReferenceId ON [v_Event] (EventType, ReferenceId)
GO

ZZZ Projects - Sponsorship

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.

Entity Framework Extensions - Sponsor

Dapper Plus - Sponsor

No packages depend on Audit.NET.SqlServer.

Version Downloads Last updated
31.0.1 2 09/03/2025
31.0.0 2 09/04/2025
30.1.3 2 09/03/2025
30.1.2 2 09/04/2025
30.1.1 1 09/04/2025
30.0.2 7 07/02/2025
30.0.1 9 05/30/2025
30.0.0 8 05/29/2025
29.0.1 8 05/11/2025
29.0.0 7 05/08/2025
28.0.0 9 05/09/2025
27.5.3 9 04/24/2025
27.5.2 8 05/30/2025
27.5.1 9 04/06/2025
27.5.0 12 03/05/2025
27.4.1 13 02/11/2025
27.4.0 12 02/11/2025
27.3.3 14 02/11/2025
27.3.2 12 12/18/2024
27.3.1 15 12/18/2024
27.3.0 12 12/18/2024
27.2.0 15 11/29/2024
27.1.1 15 10/31/2024
27.1.0 13 10/27/2024
27.0.3 16 09/26/2024
27.0.2 20 09/19/2024
27.0.1 17 09/05/2024
27.0.0 18 09/05/2024
26.0.1 17 08/22/2024
26.0.0 19 07/21/2024
25.0.7 18 07/06/2024
25.0.6 17 06/25/2024
25.0.5 20 06/21/2024
25.0.4 18 04/14/2024
25.0.3 22 03/15/2024
25.0.2 21 03/12/2024
25.0.1 19 03/12/2024
25.0.0 21 03/12/2024
24.0.1 23 02/12/2024
24.0.0 21 02/12/2024
23.0.0 23 01/19/2024
22.1.0 25 02/11/2024
22.0.2 19 02/11/2024
22.0.1 21 02/11/2024
22.0.0 22 02/11/2024
21.1.0 25 12/04/2023
21.0.4 21 01/08/2024
21.0.3 21 02/12/2024
21.0.2 21 12/21/2023
21.0.1 21 12/24/2023
21.0.0 21 11/08/2023
20.2.4 27 12/22/2023
20.2.3 25 12/25/2023
20.2.2 23 12/04/2023
20.2.1 24 12/17/2023
20.2.0 24 12/14/2023
20.1.6 20 12/21/2023
20.1.5 18 12/25/2023
20.1.4 17 02/11/2024
20.1.3 21 12/22/2023
20.1.2 20 12/22/2023
20.1.1 18 02/11/2024
20.1.0 25 12/10/2023
20.0.4 25 12/23/2023
20.0.3 21 12/10/2023
20.0.2 23 12/12/2023
20.0.1 24 11/08/2023
20.0.0 21 12/10/2023
19.4.1 22 12/14/2023
19.4.0 21 12/03/2023
19.3.0 28 12/04/2023
19.2.2 25 12/04/2023
19.2.1 22 12/23/2023
19.2.0 19 12/21/2023
19.1.4 21 12/17/2023
19.1.3 22 12/22/2023
19.1.2 29 12/10/2023
19.1.1 21 12/11/2023
19.1.0 25 12/14/2023
19.0.7 125 03/31/2022
19.0.6 25 03/09/2022
19.0.5 19 12/28/2023
19.0.4 18 02/03/2024
19.0.3 29 01/09/2024
19.0.2 26 01/10/2024
19.0.1 18 12/05/2023
19.0.0 22 11/19/2021
19.0.0-rc.net60.2 18 02/12/2024
19.0.0-rc.net60.1 16 02/12/2024
18.1.6 19 02/11/2024
18.1.5 165 09/24/2021
18.1.4 32 12/23/2023
18.1.3 19 02/11/2024
18.1.2 24 12/15/2023
18.1.1 21 02/11/2024
18.1.0 18 02/11/2024
18.0.1 20 12/09/2023
18.0.0 19 02/11/2024
17.0.8 19 12/19/2023
17.0.7 27 01/12/2024
17.0.6 19 12/06/2023
17.0.5 19 12/10/2023
17.0.4 23 12/24/2023
17.0.3 26 12/10/2023
17.0.2 21 12/11/2023
17.0.1 20 12/17/2023
17.0.0 21 12/15/2023
16.5.6 30 12/25/2023
16.5.5 21 12/24/2023
16.5.4 23 12/11/2023
16.5.3 21 12/23/2023
16.5.2 19 12/27/2023
16.5.1 21 12/03/2023
16.5.0 24 12/02/2023
16.4.5 18 12/23/2023
16.4.4 23 02/11/2024
16.4.3 22 12/28/2023
16.4.2 20 12/07/2023
16.4.1 22 12/05/2023
16.4.0 22 12/27/2023
16.3.3 23 12/08/2023
16.3.2 20 03/12/2024
16.3.1 19 02/11/2024
16.3.0 21 12/28/2023
16.2.1 21 12/25/2023
16.2.0 19 02/11/2024
16.1.5 26 12/13/2023
16.1.4 36 01/28/2024
16.1.3 21 12/30/2023
16.1.2 21 12/22/2023
16.1.1 18 12/25/2023
16.1.0 20 12/21/2023
16.0.3 23 12/22/2023
16.0.2 26 12/19/2023
16.0.1 24 12/18/2023
16.0.0 21 12/03/2023
15.3.0 20 12/09/2023
15.2.3 20 12/11/2023
15.2.2 22 12/03/2023
15.2.1 21 12/10/2023
15.2.0 20 01/01/2024
15.1.1 21 12/02/2023
15.1.0 20 12/25/2023
15.0.5 21 12/03/2023
15.0.4 25 12/28/2023
15.0.3 20 12/13/2023
15.0.2 18 02/11/2024
15.0.1 18 12/15/2023
15.0.0 21 12/16/2023
14.9.1 22 02/12/2024
14.9.0 20 12/10/2023
14.8.1 29 12/20/2023
14.8.0 22 12/21/2023
14.7.0 18 12/19/2023
14.6.6 20 12/27/2023
14.6.5 24 12/13/2023
14.6.4 22 12/11/2023
14.6.3 19 02/11/2024
14.6.2 20 12/22/2023
14.6.1 24 12/26/2023
14.6.0 19 12/20/2023
14.5.7 20 12/27/2023
14.5.6 21 12/11/2023
14.5.5 23 12/05/2023
14.5.4 22 12/11/2023
14.5.3 18 01/10/2024
14.5.2 18 12/08/2023
14.5.1 22 02/11/2024
14.5.0 19 12/11/2023
14.4.0 20 12/04/2023
14.3.4 23 12/11/2023
14.3.3 24 02/11/2024
14.3.2 20 12/13/2023
14.3.1 20 12/25/2023
14.3.0 26 12/11/2023
14.2.3 19 12/07/2023
14.2.2 19 02/11/2024
14.2.1 21 02/11/2024
14.2.0 23 12/28/2023
14.1.1 21 12/22/2023
14.1.0 24 12/12/2023
14.0.4 22 01/01/2024
14.0.3 27 12/31/2023
14.0.2 17 01/02/2024
14.0.1 22 12/15/2023
14.0.0 21 12/31/2023
13.3.0 21 12/20/2023
13.2.2 23 12/26/2023
13.2.1 20 12/18/2023
13.2.0 19 01/12/2024
13.1.5 22 01/14/2024
13.1.4 23 12/19/2023
13.1.3 23 01/13/2024
13.1.2 24 02/11/2024
13.1.1 25 12/21/2023
13.1.0 21 12/07/2023
13.0.0 25 12/21/2023
12.3.6 24 12/24/2023
12.3.5 24 12/06/2023
12.3.4 21 12/25/2023
12.3.3 23 12/26/2023
12.3.2 30 02/11/2024
12.3.1 19 12/17/2023
12.3.0 18 02/11/2024
12.2.2 21 02/11/2024
12.2.1 23 12/11/2023
12.2.0 22 12/19/2023
12.1.11 20 12/19/2023
12.1.10 22 12/24/2023
12.1.9 26 12/14/2023
12.1.8 20 12/10/2023
12.1.7 24 12/21/2023
12.1.6 21 12/04/2023
12.1.5 21 01/11/2024
12.1.4 23 02/11/2024
12.1.3 23 12/11/2023
12.1.2 20 02/11/2024
12.1.1 21 12/21/2023
12.1.0 18 12/07/2023
12.0.7 26 01/02/2024
12.0.6 21 12/09/2023
12.0.5 21 12/26/2023
12.0.4 21 12/05/2023
12.0.3 18 12/08/2023
12.0.2 22 12/05/2023
12.0.1 21 11/08/2023
12.0.0 21 12/02/2023
11.2.0 22 12/19/2023
11.1.0 20 12/11/2023
11.0.8 22 12/27/2023
11.0.7 26 12/18/2023
11.0.6 19 12/20/2023
11.0.5 21 02/11/2024
11.0.4 21 12/08/2023
11.0.3 23 12/18/2023
11.0.2 31 12/14/2023
11.0.1 21 12/13/2023
11.0.0 18 02/11/2024
10.0.3 19 02/13/2024
10.0.2 20 12/22/2023
10.0.1 22 12/14/2023
10.0.0 25 11/19/2023
9.3.0 21 12/14/2023
9.2.0 20 02/12/2024
9.1.3 28 12/09/2023
9.1.2 22 12/13/2023
9.1.1 23 12/12/2023
9.1.0 21 12/17/2023
9.0.1 22 12/15/2023
9.0.0 19 12/18/2023
8.7.0 21 11/09/2023
8.6.0 24 12/21/2023
8.5.0 22 12/26/2023
8.4.0 22 12/25/2023
8.3.1 23 12/13/2023
8.3.0 21 12/12/2023
8.2.0 28 12/21/2023
8.1.0 22 12/03/2023
8.0.0 22 12/24/2023
7.1.3 19 12/29/2023
7.1.2 21 02/12/2024
7.1.1 19 12/02/2023
7.1.0 20 12/08/2023
7.0.9 19 12/22/2023
7.0.8 22 12/22/2023
7.0.6 19 12/08/2023
7.0.5 19 12/26/2023
7.0.4 22 12/03/2023
7.0.3 23 01/08/2024
7.0.2 22 12/17/2023
7.0.0 19 12/15/2023
6.2.0 23 12/24/2023
6.1.0 20 12/27/2023
6.0.0 20 12/03/2023
5.3.0 17 12/20/2023
5.2.0 22 12/23/2023
5.1.0 23 12/17/2023
5.0.0 16 12/20/2023
4.11.0 21 12/25/2023
4.10.0 18 12/15/2023
4.9.0 22 12/03/2023
4.8.0 30 12/27/2023
4.7.0 18 12/19/2023
4.6.5 21 12/26/2023
4.6.4 20 12/15/2023
4.6.2 23 12/09/2023
4.6.1 23 12/08/2023
4.6.0 21 12/11/2023
4.5.9 24 02/13/2024
4.5.8 20 01/03/2024
4.5.7 18 12/13/2023
4.5.6 19 12/04/2023
4.5.5 24 12/09/2023
4.5.4 20 12/21/2023
4.5.3 21 12/20/2023
4.5.2 21 12/25/2023
4.5.1 23 12/10/2023
4.5.0 20 12/26/2023
4.4.0 17 12/21/2023
4.3.0 23 12/23/2023
4.2.0 20 12/21/2023
4.1.0 23 12/13/2023
4.0.1 25 12/20/2023
4.0.0 17 12/28/2023
3.6.0 22 12/02/2023
3.4.0 19 12/23/2023
3.3.0 21 12/03/2023
3.2.0 21 02/12/2024
3.1.0 26 12/12/2023
3.0.0 23 12/22/2023
2.6.0 22 12/26/2023
2.5.0 18 12/13/2023
2.4.0 19 12/19/2023
2.3.0 23 12/23/2023
2.1.0 19 12/19/2023
2.0.0 23 12/11/2023
1.0.0.4 16 12/08/2023
1.0.0.3 22 12/10/2023
1.0.0.2 18 12/26/2023
1.0.0.1 19 02/12/2024
1.0.0 25 12/19/2023