|
| 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