Audit.NET.SqlServer 22.0.2

.NET 5.0 .NET Standard 1.3 .NET Framework 4.5
dotnet add package Audit.NET.SqlServer --version 22.0.2
NuGet\Install-Package Audit.NET.SqlServer -Version 22.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Audit.NET.SqlServer" Version="22.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Audit.NET.SqlServer --version 22.0.2
#r "nuget: Audit.NET.SqlServer, 22.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Audit.NET.SqlServer as a Cake Addin
#addin nuget:?package=Audit.NET.SqlServer&version=22.0.2

// Install Audit.NET.SqlServer as a Cake Tool
#tool nuget:?package=Audit.NET.SqlServer&version=22.0.2

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 when you need more control or configuration over the DB connection. (optional, only for .NET Framework)
  • 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)
  • SetDatabaseInitializerNull: To set the database initializer to NULL on the internal DbContext. (optional, only for .NET Framework)
  • DbContextOptions: To set custom options for the internal EntityFramework DbContext (optional, only for .NET Core)

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
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.3 is compatible.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Audit.NET.SqlServer:

Package Downloads
RezisFramework

Package Description

Jetstream.Infrastructure.Audit

Project that contains the classes and interfaces used in creating a base auditing structure for API projects.

Tamimi.QService.Audit

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
22.0.2 138 12/1/2023
22.0.1 768 11/16/2023
22.0.0 206 11/14/2023
21.1.0 5,100 10/9/2023
21.0.4 3,689 9/15/2023
21.0.3 17,304 7/9/2023
21.0.2 304 7/6/2023
21.0.1 9,164 5/27/2023
21.0.0 8,460 4/15/2023
20.2.4 12,204 3/27/2023
20.2.3 2,157 3/17/2023
20.2.2 1,470 3/14/2023
20.2.1 2,740 3/11/2023
20.2.0 584 3/7/2023
20.1.6 2,535 2/23/2023
20.1.5 20,348 2/9/2023
20.1.4 2,976 1/28/2023
20.1.3 10,335 12/21/2022
20.1.2 10,074 12/14/2022
20.1.1 684 12/12/2022
20.1.0 965 12/4/2022
20.0.4 1,764 11/30/2022
20.0.3 5,996 10/28/2022
20.0.2 1,166 10/26/2022
20.0.1 1,014 10/21/2022
20.0.0 94,178 10/1/2022
19.4.1 17,040 9/10/2022
19.4.0 8,149 9/2/2022
19.3.0 12,630 8/23/2022
19.2.2 4,222 8/11/2022
19.2.1 3,226 8/6/2022
19.2.0 6,436 7/24/2022
19.1.4 25,626 5/23/2022
19.1.3 639 5/22/2022
19.1.2 741 5/18/2022
19.1.1 11,398 4/28/2022
19.1.0 6,438 4/10/2022
19.0.7 24,284 3/13/2022
19.0.6 2,728 3/7/2022
19.0.5 55,082 1/28/2022
19.0.4 3,455 1/23/2022
19.0.3 23,729 12/14/2021
19.0.2 562 12/11/2021
19.0.1 9,743 11/20/2021
19.0.0 2,779 11/11/2021
19.0.0-rc.net60.2 273 9/26/2021
19.0.0-rc.net60.1 216 9/16/2021
18.1.6 16,229 9/26/2021
18.1.5 4,138 9/7/2021
18.1.4 1,063 9/6/2021
18.1.3 4,253 8/19/2021
18.1.2 2,336 8/8/2021
18.1.1 1,033 8/5/2021
18.1.0 3,031 8/1/2021
18.0.1 2,325 7/30/2021
18.0.0 1,886 7/26/2021
17.0.8 3,360 7/7/2021
17.0.7 6,500 6/16/2021
17.0.6 1,495 6/5/2021
17.0.5 1,872 5/28/2021
17.0.4 6,249 5/4/2021
17.0.3 823 5/1/2021
17.0.2 1,002 4/22/2021
17.0.1 1,694 4/18/2021
17.0.0 4,923 3/26/2021
16.5.6 2,683 3/25/2021
16.5.5 1,699 3/23/2021
16.5.4 2,144 3/9/2021
16.5.3 7,209 2/26/2021
16.5.2 1,226 2/23/2021
16.5.1 753 2/21/2021
16.5.0 639 2/17/2021
16.4.5 729 2/15/2021
16.4.4 4,298 2/5/2021
16.4.3 5,455 1/27/2021
16.4.2 3,375 1/22/2021
16.4.1 1,643 1/21/2021
16.4.0 12,549 1/11/2021
16.3.3 703 1/8/2021
16.3.2 970 1/3/2021
16.3.1 874 12/31/2020
16.3.0 13,538 12/30/2020
16.2.1 2,509 12/27/2020
16.2.0 26,680 10/13/2020
16.1.5 3,733 10/4/2020
16.1.4 4,076 9/17/2020
16.1.3 1,564 9/13/2020
16.1.2 1,046 9/9/2020
16.1.1 1,725 9/3/2020
16.1.0 3,512 8/19/2020
16.0.3 759 8/15/2020
16.0.2 6,999 8/9/2020
16.0.1 950 8/8/2020
16.0.0 10,059 8/7/2020
15.3.0 34,956 7/23/2020
15.2.3 2,672 7/14/2020
15.2.2 34,245 5/19/2020
15.2.1 7,680 5/12/2020
15.2.0 1,498 5/9/2020
15.1.1 14,425 5/4/2020
15.1.0 4,259 4/13/2020
15.0.5 16,739 3/18/2020
15.0.4 2,396 2/28/2020
15.0.3 1,149 2/26/2020
15.0.2 6,562 1/20/2020
15.0.1 1,820 1/10/2020
15.0.0 4,281 12/17/2019
14.9.1 6,884 11/30/2019
14.9.0 749 11/29/2019
14.8.1 1,123 11/26/2019
14.8.0 11,131 11/20/2019
14.7.0 31,922 10/9/2019
14.6.6 823 10/8/2019
14.6.5 4,606 9/27/2019
14.6.4 721 9/21/2019
14.6.3 12,570 8/12/2019
14.6.2 2,261 8/3/2019
14.6.1 342 8/3/2019
14.6.0 15,163 7/26/2019
14.5.7 3,326 7/18/2019
14.5.6 4,035 7/10/2019
14.5.5 1,187 7/1/2019
14.5.4 1,212 6/17/2019
14.5.3 15,102 6/5/2019
14.5.2 18,598 5/30/2019
14.5.1 1,284 5/28/2019
14.5.0 1,412 5/24/2019
14.4.0 1,847 5/22/2019
14.3.4 3,553 5/14/2019
14.3.3 2,536 5/9/2019
14.3.2 1,278 4/30/2019
14.3.1 920 4/27/2019
14.3.0 1,073 4/24/2019
14.2.3 6,881 4/17/2019
14.2.2 984 4/10/2019
14.2.1 912 4/5/2019
14.2.0 3,341 3/16/2019
14.1.1 908 3/8/2019
14.1.0 4,724 2/11/2019
14.0.4 2,117 1/31/2019
14.0.3 1,578 1/22/2019
14.0.2 10,289 12/15/2018
14.0.1 2,149 11/29/2018
14.0.0 1,352 11/19/2018
13.3.0 1,101 11/16/2018
13.2.2 986 11/15/2018
13.2.1 1,716 11/13/2018
13.2.0 2,321 10/31/2018
13.1.5 1,114 10/31/2018
13.1.4 7,060 10/25/2018
13.1.3 1,256 10/18/2018
13.1.2 4,454 9/12/2018
13.1.1 1,313 9/11/2018
13.1.0 1,011 9/11/2018
13.0.0 7,024 8/29/2018
12.3.6 1,133 8/29/2018
12.3.5 32,918 8/22/2018
12.3.4 1,134 8/21/2018
12.3.3 25,917 8/21/2018
12.3.2 1,159 8/20/2018
12.3.1 1,118 8/20/2018
12.3.0 1,087 8/20/2018
12.2.2 2,462 8/15/2018
12.2.1 3,542 8/9/2018
12.2.0 1,182 8/8/2018
12.1.11 1,902 7/30/2018
12.1.10 1,200 7/20/2018
12.1.9 1,506 7/10/2018
12.1.8 1,335 7/2/2018
12.1.7 4,616 6/7/2018
12.1.6 3,679 6/4/2018
12.1.5 1,524 6/2/2018
12.1.4 1,622 5/25/2018
12.1.3 2,648 5/16/2018
12.1.2 1,376 5/15/2018
12.1.1 1,389 5/14/2018
12.1.0 1,784 5/9/2018
12.0.7 1,465 5/5/2018
12.0.6 1,409 5/4/2018
12.0.5 1,371 5/3/2018
12.0.4 1,405 4/30/2018
12.0.3 1,453 4/30/2018
12.0.2 1,391 4/27/2018
12.0.1 1,528 4/25/2018
12.0.0 1,252 4/22/2018
11.2.0 1,879 4/11/2018
11.1.0 9,943 4/8/2018
11.0.8 1,471 3/26/2018
11.0.7 1,406 3/20/2018
11.0.6 4,773 3/7/2018
11.0.5 1,505 2/22/2018
11.0.4 2,289 2/14/2018
11.0.3 1,421 2/12/2018
11.0.2 1,444 2/9/2018
11.0.1 2,817 1/29/2018
11.0.0 2,021 1/15/2018
10.0.3 2,270 12/29/2017
10.0.2 1,286 12/26/2017
10.0.1 1,480 12/18/2017
10.0.0 1,249 12/18/2017
9.3.0 1,370 12/17/2017
9.2.0 1,244 12/17/2017
9.1.3 3,557 12/5/2017
9.1.2 1,544 11/27/2017
9.1.1 1,933 11/21/2017
9.1.0 1,254 11/21/2017
9.0.1 1,364 11/11/2017
9.0.0 1,251 11/10/2017
8.7.0 1,344 11/9/2017
8.6.0 1,237 11/9/2017
8.5.0 9,024 10/3/2017
8.4.0 1,233 10/3/2017
8.3.1 4,856 9/8/2017
8.3.0 1,288 9/8/2017
8.2.0 1,308 9/4/2017
8.1.0 1,940 8/22/2017
8.0.0 1,469 8/19/2017
7.1.3 1,387 8/14/2017
7.1.2 1,743 8/2/2017
7.1.1 1,544 7/26/2017
7.1.0 2,482 7/5/2017
7.0.9 1,404 6/28/2017
7.0.8 1,502 6/19/2017
7.0.6 11,045 4/7/2017
7.0.5 1,446 3/21/2017
7.0.4 1,377 3/21/2017
7.0.3 1,378 3/20/2017
7.0.2 1,471 3/13/2017
7.0.0 1,561 3/1/2017
6.2.0 3,069 2/25/2017
6.1.0 5,418 2/14/2017
6.0.0 1,385 2/9/2017
5.3.0 1,242 2/5/2017
5.2.0 1,754 1/26/2017
5.1.0 1,247 1/19/2017
5.0.0 1,251 1/7/2017
4.11.0 1,216 1/5/2017
4.10.0 1,191 12/31/2016
4.9.0 1,221 12/26/2016
4.8.0 1,277 12/17/2016
4.7.0 1,331 12/8/2016
4.6.5 1,288 12/4/2016
4.6.4 1,340 11/25/2016
4.6.2 1,336 11/18/2016
4.6.1 1,264 11/15/2016
4.6.0 1,214 11/11/2016
4.5.9 1,674 11/2/2016
4.5.8 1,234 11/2/2016
4.5.7 1,284 10/26/2016
4.5.6 1,349 10/6/2016
4.5.5 1,283 10/3/2016
4.5.4 1,250 10/2/2016
4.5.3 1,222 9/30/2016
4.5.2 1,239 9/28/2016
4.5.1 1,196 9/28/2016
4.5.0 1,217 9/28/2016
4.4.0 1,358 9/23/2016
4.3.0 1,350 9/22/2016
4.2.0 1,518 9/19/2016
4.1.0 1,243 9/13/2016
4.0.1 1,193 9/9/2016
4.0.0 1,208 9/9/2016
3.6.0 1,224 9/7/2016
3.4.0 1,243 9/7/2016
3.3.0 1,216 9/4/2016
3.2.0 1,191 9/3/2016
3.1.0 1,256 9/2/2016
3.0.0 1,493 8/31/2016
2.6.0 1,269 8/29/2016
2.5.0 1,253 8/27/2016
2.4.0 1,516 8/26/2016
2.3.0 1,183 8/23/2016
2.1.0 1,233 8/22/2016
2.0.0 1,264 8/21/2016
1.0.0.4 1,260 8/19/2016
1.0.0.3 1,484 8/18/2016
1.0.0.2 1,560 8/18/2016
1.0.0.1 1,238 8/18/2016
1.0.0 1,482 8/18/2016