|
| 1 | +.. _csharp-monitoring: |
| 2 | + |
| 3 | +========== |
| 4 | +Monitoring |
| 5 | +========== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: event, subscribe, listener |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +On this page, you can learn how to configure **monitoring** in the |
| 24 | +{+driver-long+}. Monitoring is the process of gathering information about your |
| 25 | +application's performance and resource usage as it runs. |
| 26 | +This can help you make informed decisions when designing and debugging your application. |
| 27 | + |
| 28 | +The driver provides information about your application by emitting events. You can |
| 29 | +subscribe to these driver events to monitor your application. |
| 30 | + |
| 31 | +.. note:: Event Logging |
| 32 | + |
| 33 | + This page explains how to monitor your application in code. To learn how to record |
| 34 | + this information to an external log, see :ref:`Logging. <csharp-logging>` |
| 35 | + |
| 36 | +.. _csharp-event-types: |
| 37 | + |
| 38 | +Event Types |
| 39 | +----------- |
| 40 | + |
| 41 | +The type of event that the driver emits depends on the operation being performed. |
| 42 | +The following table describes the types of events that the driver emits: |
| 43 | + |
| 44 | +.. list-table:: |
| 45 | + :header-rows: 1 |
| 46 | + :widths: 30 70 |
| 47 | + |
| 48 | + * - Event Type |
| 49 | + - Description |
| 50 | + * - Command Events |
| 51 | + - Events related to MongoDB database commands, such as ``find``, ``insert``, |
| 52 | + ``delete``, and ``count``. To learn how to use the {+driver-short+} to run a |
| 53 | + database command, see :ref:`<csharp-run-command>`. For more information about |
| 54 | + MongoDB database commands, see :manual:`Database Commands </reference/command/>` |
| 55 | + in the {+mdb-server+} manual. |
| 56 | + |
| 57 | + As a security measure, the driver redacts the contents of some |
| 58 | + command events. This protects the sensitive information contained in these command |
| 59 | + events. |
| 60 | + |
| 61 | + * - Server Discovery and Monitoring (SDAM) Events |
| 62 | + - Events related to changes in the state of the MongoDB deployment. |
| 63 | + |
| 64 | + * - Connection Pool Events |
| 65 | + - Events related to the connection pool held by the driver. |
| 66 | + |
| 67 | +For a complete list of events the driver emits, see the API documentation for the |
| 68 | +`MongoDB.Driver.Core.Events <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Core.Events.html>`__ |
| 69 | +namespace. |
| 70 | + |
| 71 | +.. _csharp-monitor-events: |
| 72 | + |
| 73 | +Subscribing to Events |
| 74 | +--------------------- |
| 75 | + |
| 76 | +To monitor an event, you must subscribe a listener method on your ``MongoClient`` instance. |
| 77 | +The following steps describe how to subscribe to events: |
| 78 | + |
| 79 | +1. Create a ``MongoClientSettings`` object. |
| 80 | +#. Set the ``ClusterConfigurator`` property on the ``MongoClientSettings`` object to a |
| 81 | + lambda function that accepts a ``ClusterBuilder`` object. |
| 82 | +#. In the lambda function, call the ``Subscribe<TEvent>()`` |
| 83 | + method on the ``ClusterBuilder`` object for each event you want to subscribe to. |
| 84 | + Replace ``TEvent`` with the event type. Pass the event handler |
| 85 | + method as an argument to the ``Subscribe<TEvent>()`` method. |
| 86 | + |
| 87 | +The following code example shows how to subscribe to the ``ClusterOpenedEvent``, |
| 88 | +``ServerHeartbeatSucceededEvent``, and ``ConnectionPoolReadyEvent``. This example |
| 89 | +assumes that the ``ClusterEventHandler``, ``HeartbeatEventHandler``, |
| 90 | +and ``ConnectionPoolEventHandler`` methods are defined elsewhere in your code. |
| 91 | + |
| 92 | +.. code-block:: csharp |
| 93 | + |
| 94 | + var clientSettings = MongoClientSettings.FromConnectionString(MongoConnectionString); |
| 95 | + clientSettings.ClusterConfigurator = clusterBuilder => |
| 96 | + { |
| 97 | + clusterBuilder |
| 98 | + .Subscribe<ClusterOpenedEvent>(ClusterEventHandler) |
| 99 | + .Subscribe<ServerHeartbeatSucceededEvent>(HeartbeatEventHandler) |
| 100 | + .Subscribe<ConnectionPoolReadyEvent>(ConnectionPoolEventHandler); |
| 101 | + }; |
| 102 | + |
| 103 | +.. tip:: |
| 104 | + |
| 105 | + You can subscribe to any number of events, and these events can be of different types. |
| 106 | + |
| 107 | +API Documentation |
| 108 | +----------------- |
| 109 | + |
| 110 | +To learn more about the methods and classes used to monitor events in the driver, see the |
| 111 | +following API documentation: |
| 112 | + |
| 113 | +- `MongoDB.Driver.Core.Events <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Core.Events.html>`__ |
| 114 | +- `Subscribe<TEvent>() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Core.Configuration.ClusterBuilder.Subscribe.html>`__ |
0 commit comments