Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversC#/.NET

Logging

On this page

  • Overview
  • Configure Logging
  • Log Messages by Category
  • Configure Log Verbosity

Starting in version 2.18, the .NET/C# Driver uses the standard .NET logging API. In this guide, you can learn how to use the driver to configure logging for your application.

Important

To use this feature, you must add the Microsoft.Extensions.Logging.Console package to your project.

To specify the logging settings for your application, create a new instance of the LoggingSettings class, then assign it to the LoggingSettings property of your MongoClientSettings object.

The LoggingSettings constructor accepts the following parameters:

Property
Description
LoggerFactory
The ILoggerFactory object that creates an ILogger. You can create an ILoggerFactory object by using the LoggerFactory.Create() method.

Data Type: ILoggerFactory
Default: null
MaxDocumentSize
Optional. The maximum number of characters for extended JSON documents in logged messages.

For example, when the driver logs the CommandStarted message, it truncates the Command field to the number of characters specified in this parameter.

Data Type: integer
Default: 1000

The following code sample shows how to create a MongoClient that logs all debug messages to the console:

using var loggerFactory = LoggerFactory.Create(b =>
{
b.AddSimpleConsole();
b.SetMinimumLevel(LogLevel.Debug);
});
var settings = MongoClientSettings.FromConnectionString("<connection string>");
settings.LoggingSettings = new LoggingSettings(loggerFactory);
var client = new MongoClient(settings);

Each message generated by a MongoDB cluster is assigned a category. This lets you specify different log levels for different types of messages.

MongoDB uses the following categories to classify messages:

Category
Description
MongoDB.Command
The progress of commands run against your cluster, represented by CommandStartedEvent, CommandSucceededEvent, and CommandFailedEvent
MongoDB.SDAM
Changes in the topology of the cluster, including ClusterAddedServerEvent, ClusterRemovedServerEvent, ServerHeartbeatStartedEvent, ClusterDescriptionChangedEvent, and ServerDescriptionChangedEvent
MongoDB.ServerSelection
The decisions that determine which server to send a particular command to
MongoDB.Connection
Changes in the cluster connection pool, including ConnectionPoolReadyEvent, ConnectionPoolClosedEvent, ConnectionCreatedEvent, and ConnectionCheckoutEvent
MongoDB.Internal.*
Prefix for all other .NET/C# Driver internal components

Tip

You can specify the minimum verbosity for all logging categories by configuring the Default category.

You can configure the log verbosity of each message category by using the standard .NET logging mechanism. The following code sample shows how to configure a MongoClient to log two types of messages:

  • All messages with log level Error or higher from all categories

  • All messages with log level Debug or higher from the SDAM category

In this example, the configuration is done in-memory. The code creates a Dictionary<string, string> where the key is "LogLevel:<category>" and the value is the minimum log level of messages in that category. The code then adds the dictionary to a ConfigurationBuilder object, then adds the ConfigurationBuilder to a LoggerFactory.

var categoriesConfiguration = new Dictionary<string, string>
{
{ "LogLevel:Default", "Error" },
{ "LogLevel:MongoDB.SDAM", "Debug" }
};
var config = new ConfigurationBuilder()
.AddInMemoryCollection(categoriesConfiguration)
.Build();
using var loggerFactory = LoggerFactory.Create(b =>
{
b.AddConfiguration(config);
b.AddSimpleConsole();
});
var settings = MongoClientSettings.FromConnectionString("<connection string>");
settings.LoggingSettings = new LoggingSettings(loggerFactory);
var client = new MongoClient(settings);

Tip

For more information on configuring log verbosity, see the Microsoft .NET logging documentation.

← GUIDs