Skip to content

Commit d4e4eac

Browse files
authored
CSHARP-4387: Logging documentation (#963)
1 parent b85c219 commit d4e4eac

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

Docs/reference/content/reference/driver/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ The MongoDB .NET Driver is mostly just a wrapper around [MongoDB.Driver.Core]({{
1919
- [Administration]({{< relref "reference\driver\admin.md" >}})
2020
- [Definitions and Builders]({{< relref "reference\driver\definitions.md" >}})
2121
- [CRUD Operations]({{< relref "reference\driver\crud\index.md" >}})
22-
- [Error Handling]({{< relref "reference\driver\error_handling.md" >}})
22+
- [Error Handling]({{< relref "reference\driver\error_handling.md" >}})
23+
- [Logging]({{< relref "reference\driver\logging.md" >}})
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
+++
2+
date = "2022-11-22T15:36:56Z"
3+
draft = false
4+
title = "Logging"
5+
[menu.main]
6+
parent = "Driver"
7+
identifier = "Logging"
8+
weight = 10
9+
pre = "<i class='fa'></i>"
10+
+++
11+
12+
## Logging
13+
14+
Starting in version 2.18, the .NET/C# driver uses the standard [.NET logging API](https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line). The [MongoDB logging specification](https://github.com/mongodb/specifications/blob/master/source/logging/logging.rst) defines the components, structure, and verbosity of the logs. On this page, you can learn how to set up and configure logging for your application.
15+
16+
You can configure logging using the [`LoggingSettings`]({{< apiref "T_MongoDB_Driver_Core_Configuration_LoggingSettings" >}}). ```LoggingSettings``` contains the following properties:
17+
18+
|Property|Description|
19+
|--------|-----------|
20+
|LoggerFactory|The [ILoggerFactory](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.iloggerfactory?view=dotnet-plat-ext-6.0) object that will create an [ILogger](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.ilogger?view=dotnet-plat-ext-6.0).<br><br>**Data Type:** ILoggerFactory<br>**Default:** null |
21+
|MaxDocumentSize|Maximum number of characters for extended JSON documents in logged messages<br><br>For example, when the driver logs the [CommandStarted](https://github.com/mongodb/specifications/blob/master/source/command-logging-and-monitoring/command-logging-and-monitoring.rst#command-started-message) event, it truncates the Command field to the specified character limit.<br><br>**Data Type:** int<br>**Default:** 1000|
22+
23+
24+
The following code example creates a MongoClient that logs debug messages to the console. To do so, the code performs the following steps:
25+
- Creates a LoggerFactory, which specifies the logging destination and level
26+
- Creates a LoggingSettings object, passing the LoggerFactory object as a parameter to the constructor
27+
- Creates a MongoClient object, passing the LoggingSettings object as a parameter to the constructor
28+
29+
30+
```csharp
31+
using var loggerFactory = LoggerFactory.Create(b =>
32+
{
33+
b.AddSimpleConsole();
34+
b.SetMinimumLevel(LogLevel.Debug);
35+
});
36+
37+
var settings = MongoClientSettings.FromConnectionString(...);
38+
settings.LoggingSettings = new LoggingSettings(loggerFactory);
39+
var client = new MongoClient(settings);
40+
```
41+
.NET/C# driver log category naming:
42+
43+
|Property|Description|
44+
|--------|-----------|
45+
|MongoDB.Command|command|
46+
|MongoDB.SDAM|topology|
47+
|MongoDB.ServerSelection|serverSelection|
48+
|MongoDB.Connection|connection|
49+
|MongoDB.Internal.*|Prefix for all .NET/C# Driver internal components not described by spec|
50+
51+
52+
How to configure log categories verbosity example:
53+
54+
```csharp
55+
var categoriesConfiguration = new Dictionary<string, string>
56+
{
57+
// Output all logs from all categories with Error and higher level
58+
{ "LogLevel:Default", "Error" },
59+
// Output SDAM logs with Debug and higher level
60+
{ "LogLevel:MongoDB.SDAM", "Debug" }
61+
};
62+
var config = new ConfigurationBuilder()
63+
.AddInMemoryCollection(categoriesConfiguration)
64+
.Build();
65+
using var loggerFactory = LoggerFactory.Create(b =>
66+
{
67+
b.AddConfiguration(config);
68+
b.AddSimpleConsole();
69+
});
70+
71+
var settings = MongoClientSettings.FromConnectionString("mongodb://localhost:27017");
72+
settings.LoggingSettings = new LoggingSettings(loggerFactory);
73+
var client = new MongoClient(settings);
74+
```

0 commit comments

Comments
 (0)