-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathlogging.txt
165 lines (123 loc) · 5.42 KB
/
logging.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
.. _csharp-logging:
=======
Logging
=======
.. facet::
:name: genre
:values: reference
.. meta::
:keywords: loggerfactory, logger, audit
:description: Learn to configure logging in the .NET/C# Driver using the standard .NET logging API, including setting log verbosity and categories.
.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol
Overview
--------
Starting in version 2.18, the {+driver-short+} uses the standard
`.NET logging API. <https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line>`__
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
<https://www.nuget.org/packages/Microsoft.Extensions.Logging.Console/>`__
to your project.
Configure Logging
-----------------
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:
.. list-table::
:header-rows: 1
:widths: 25 75
* - 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 <https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.iloggerfactory?view=dotnet-plat-ext-7.0>`__
| **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**: {+int-data-type+}
| **Default**: ``1000``
The following code sample shows how to create a ``MongoClient`` that
logs all debug messages to the console:
.. code-block:: csharp
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);
Log Messages by Category
------------------------
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:
.. list-table::
:header-rows: 1
:widths: 25 75
* - 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 {+driver-short+} internal components
.. tip::
You can specify the minimum verbosity for all logging categories by configuring the
``Default`` category.
Configure Log Verbosity
-----------------------
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``.
.. code-block:: csharp
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. <https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line#configure-logging>`__