Audit.MongoClient 27.5.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Audit.MongoClient --version 27.5.3
                    
NuGet\Install-Package Audit.MongoClient -Version 27.5.3
                    
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.MongoClient" Version="27.5.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Audit.MongoClient" Version="27.5.3" />
                    
Directory.Packages.props
<PackageReference Include="Audit.MongoClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Audit.MongoClient --version 27.5.3
                    
#r "nuget: Audit.MongoClient, 27.5.3"
                    
#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.
#addin nuget:?package=Audit.MongoClient&version=27.5.3
                    
Install as a Cake Addin
#tool nuget:?package=Audit.MongoClient&version=27.5.3
                    
Install as a Cake Tool

Audit.MongoClient

MongoDB client audit extension for Audit.NET library.

Generate Audit Logs by adding a Command Event Subscriber into the configuration of the MongoDB Driver.

Audit.MongoClient provides the infrastructure to intercept a MongoClient instance, enabling the generation of audit logs for operations executed within MongoDB.

Note: This library is designed to generate audit events, not for storing events, If you're aiming to store audit events in a Mongo DB collection, you may use the Audit.NET.MongoDB package.

Install

NuGet Package

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

PM> Install-Package Audit.MongoClient

NuGet Status NuGet Count

Usage

To enable the audit log for a MongoClient instance you have to register a MongoAuditEventSubscriber instance to the ClusterBuilder.

This registration can be done in several ways:

  • Registering an instance of the provided MongoAuditEventSubscriber:
using Audit.MongoClient;
//...

var mongoSettings = new MongoClientSettings()
{
    Server = new MongoServerAddress("localhost", 27017),
    
    // Register the audit subscriber:
    ClusterConfigurator = clusterBuilder => clusterBuilder.Subscribe(new MongoAuditEventSubscriber()
    {
        IncludeReply = true
    })  
};

// Create the audited client
_client = new MongoDB.Driver.MongoClient(mongoSettings);
  • Calling the provided AddAuditSubscriber() extension method in ClusterBuilder:
using Audit.MongoClient;
//...

var mongoSettings = new MongoClientSettings()
{
    Server = new MongoServerAddress("localhost", 27017),
    ClusterConfigurator = clusterBuilder => clusterBuilder.AddAuditSubscriber(auditConfig => auditConfig
        .IncludeReply())
};

_client = new MongoDB.Driver.MongoClient(mongoSettings);
  • Reusing an existing MongoClientSettings instance by calling the provided AddAuditSubscriber() extension method:
_client = new MongoDB.Driver.MongoClient(mongoSettings.AddAuditSubscriber(cfg => cfg
    .IncludeReply());

Configuration

Output

The audit events are stored using a Data Provider. You can use one of the available data providers or implement your own. Please refer to the data providers section on Audit.NET documentation.

Settings

The MongoAuditEventSubscriber class allows to configure the following settings:

  • EventType: A string that identifies the event type. Default is "{command}". It can contain the following placeholders:
    • {command}: Replaced by the Command Name (insert, update, delete, find, ...)
  • IncludeReply: Specifies whether the command audit event should include the server reply. The reply is not included by default.
  • CommandFilter: Set a filter function to determine which command events to log depending on the command start information. By default all commands are logged.
  • CreationPolicy: Allows to set a specific event creation policy. By default the globally configured creation policy is used. See Audit.NET Event Creation Policy section for more information.
  • AuditDataProvider: Allows to set a specific audit data provider. By default the globally configured data provider is used. See Audit.NET Data Providers section for more information.
  • AuditScopeFactory: Allows to set a specific audit scope factory. By default the globally configured AuditScopeFactory is used.

You can customize these settings using the fluent API provided. Additionally, some settings can be set as functions of the executed command, allowing you to adapt the behavior based on the specific command, such as including the reply only in specific cases.

For example, to only audit insert and delete commands, and include the reply only if its length is less than 512 bytes:

var mongoSettings = new MongoClientSettings()
{
    Server = new MongoServerAddress("localhost", 27017),
    ClusterConfigurator = cc => cc
        .AddAuditSubscriber(auditConfig => auditConfig
            .IncludeReply(cmd => cmd.Reply.ToBson().Length < 512)
            .CommandFilter(cmd => cmd.CommandName is "insert" or "delete"))
};

Output Details

The following table describes the Audit.MongoClient output fields:

MongoCommandEvent

Describes a command call event

Field Name Type Description
RequestId int The unique request identifier
Connection MongoConnection Connection information
OperationId long? The operation identifier
CommandName string The Mongo command name (insert, update, delete, ...)
Body object The command body
Duration int The duration of the Mongo Event in milliseconds
Success bool? Indicates if the command succeeded
Reply object The database reply (optional)
Error string The database error message if an error occurred, otherwise NULL
Timestamp DateTime The command event Timestamp

MongoConnection

Contains the command's connection information

Field Name Type Description
ClusterId int The Connection cluster identifier
Endpoint string The Connection endpoint
LocalConnectionId long The local connection identifier
ServerConnectionId long? The server connection identifier

Output Sample

Mongo insert command:

{
	"Command": {
		"RequestId": 5,
		"Connection": {
			"ClusterId": 1,
			"Endpoint": "Unspecified/localhost:27017",
			"LocalConnectionId": 3,
			"ServerConnectionId": 55
		},
		"OperationId": 1,
		"CommandName": "insert",
		"Body": {
			"insert": "MongoClient",
			"ordered": true,
			"$db": "AuditTest",
			"lsid": {
				"id": "9498dc51-935d-4e3d-9fc0-0031d993059d"
			},
			"documents": [
				{
					"_id": "6574dcbbda3ab8f0437d1c75",
					"test": "this is a test document"
				}
			]
		},
		"Duration": 4,
		"Success": true,
		"Reply": {
			"n": 1,
			"ok": 1.0
		},
		"Timestamp": "2023-12-09T21:31:40.1286166Z"
	},
	"EventType": "3c18aa76-91cb-4c89-b575-342c9158cb44",
	"Environment": {
		"UserName": "Federico Colombo",
		"MachineName": "DESKTOP-ILAR98A",
		"DomainName": "DESKTOP-ILAR98A",
		"CallingMethodName": "Audit.MongoClient.MongoAuditEventSubscriber.Handle()",
		"AssemblyName": "Audit.MongoClient, Version=22.0.2.0, Culture=neutral, PublicKeyToken=null",
		"Culture": "en-US"
	},
	"StartDate": "2023-12-09T21:31:40.1210357Z",
	"EndDate": "2023-12-09T21:31:40.1370355Z",
	"Duration": 16
}

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

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  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 was computed.  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 was computed.  net8.0-android was computed.  net8.0-browser 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 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)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
30.0.2 136 6/26/2025
30.0.1 139 5/29/2025
30.0.0 145 5/27/2025
29.0.1 204 5/8/2025
29.0.0 161 5/6/2025
28.0.0 150 5/5/2025
27.5.3 175 4/23/2025
27.5.2 175 4/9/2025
27.5.1 88 4/4/2025
27.5.0 244 3/5/2025
27.4.1 132 2/9/2025
27.4.0 122 1/29/2025
27.3.3 117 1/21/2025
27.3.2 193 12/11/2024
27.3.1 119 12/10/2024
27.3.0 123 12/8/2024
27.2.0 143 11/23/2024
27.1.1 116 10/28/2024
27.1.0 116 10/24/2024
27.0.3 118 9/25/2024
27.0.2 140 9/19/2024
27.0.1 144 9/4/2024
27.0.0 137 9/3/2024
26.0.1 169 8/22/2024
26.0.0 912 7/19/2024
25.0.7 142 7/4/2024
25.0.6 436 6/24/2024
25.0.5 130 6/18/2024
25.0.4 600 3/24/2024
25.0.3 401 3/13/2024
25.0.2 128 3/12/2024
25.0.1 144 2/28/2024
25.0.0 146 2/16/2024
24.0.1 153 2/12/2024
24.0.0 150 2/12/2024
23.0.0 221 12/14/2023
22.1.0 170 12/9/2023