Audit.MongoClient 25.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Audit.MongoClient --version 25.0.3                
NuGet\Install-Package Audit.MongoClient -Version 25.0.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="25.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Audit.MongoClient --version 25.0.3                
#r "nuget: Audit.MongoClient, 25.0.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.
// Install Audit.MongoClient as a Cake Addin
#addin nuget:?package=Audit.MongoClient&version=25.0.3

// Install Audit.MongoClient as a Cake Tool
#tool nuget:?package=Audit.MongoClient&version=25.0.3                

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 general 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
}
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. 
.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 was computed. 
.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
27.2.0 88 11/23/2024
27.1.1 78 10/28/2024
27.1.0 80 10/24/2024
27.0.3 84 9/25/2024
27.0.2 105 9/19/2024
27.0.1 113 9/4/2024
27.0.0 103 9/3/2024
26.0.1 132 8/22/2024
26.0.0 876 7/19/2024
25.0.7 110 7/4/2024
25.0.6 209 6/24/2024
25.0.5 100 6/18/2024
25.0.4 550 3/24/2024
25.0.3 375 3/13/2024
25.0.2 101 3/12/2024
25.0.1 117 2/28/2024
25.0.0 116 2/16/2024
24.0.1 125 2/12/2024
24.0.0 119 2/12/2024
23.0.0 192 12/14/2023
22.1.0 143 12/9/2023