|
| 1 | ++++ |
| 2 | +date = "2019-04-17T21:21:00Z" |
| 3 | +draft = false |
| 4 | +title = "SDAM Events" |
| 5 | +[menu.main] |
| 6 | + parent = "Eventing" |
| 7 | + identifier = "SdamEvents" |
| 8 | + weight = 90 |
| 9 | + pre = "<i class='fa'></i>" |
| 10 | ++++ |
| 11 | + |
| 12 | +## SDAM Events |
| 13 | + |
| 14 | +Server Discovery and Monitoring (SDAM) is the process by which the driver discovers and monitors the set of servers that it is connected to. In |
| 15 | +the case of a standalone configuration the driver will only monitor a single server. In the case of a replica set configuration the driver |
| 16 | +will monitor each member of the replica set (primary, secondaries, etc.). In the case of a sharded configuration the driver will |
| 17 | +monitor the set of shard routers (mongos instances) that it is connected to. |
| 18 | + |
| 19 | +As the driver monitors the health and state of each server in a configuration, it raises a number of events that report what it is finding and |
| 20 | +how it is reacting to that information. You can subscribe to any or all of these events if you want to observe what SDAM is doing. |
| 21 | + |
| 22 | +See the general [Eventing]({{< relref "events.md" >}}) page for information on how to subscribe to individual events. |
| 23 | + |
| 24 | +### Logging SDAM Events the Easy Way |
| 25 | + |
| 26 | +Often all you want to do with SDAM events is log them. You can configure logging of SDAM events by setting the `SdamLogFilename` property of `MongoClientSettings`. |
| 27 | + |
| 28 | +```csharp |
| 29 | +var clientSettings = new MongoClientSettings(); |
| 30 | +clientSetting.SdamLogFilename = @"c:\sdam.log"; |
| 31 | +var client = new MongoClient(clientSettings); |
| 32 | +``` |
| 33 | + |
| 34 | +The `SdamLogFilename` is only configurable in code, not in the connection string. The way to combine using a connection string with SDAM logging is: |
| 35 | + |
| 36 | +```csharp |
| 37 | +var connectionString = "mongodb://localhost"; // presumably loaded via some config mechanism |
| 38 | +var clientSettings = MongoClientSettings.FromConnectionString(connectionString); |
| 39 | +clientSetting.SdamLogFilename = @"c:\sdam.log"; |
| 40 | +var client = new MongoClient(clientSettings); |
| 41 | +``` |
| 42 | + |
| 43 | +{{% note %}} |
| 44 | +Logging SDAM events results in the file being opened in exclusive mode, so it is possible for multiple instances of `MongoClient` to conflict |
| 45 | +over the use of the file. Whether the multiple `MongoClient` instances actually conflict over the use of the file or not depends on how similar |
| 46 | +the `MongoClientSettings` are. If they are different enough to result in the creation of separate underlying `Cluster` instances then they will conflict. |
| 47 | + |
| 48 | +The safest approach when logging SDAM events is to use a single instance of `MongoClient` throughout |
| 49 | +your application. This differs from previous guidance which stated that it didn't matter how many instances of `MongoClient` you created. |
| 50 | +{{% /note %}} |
| 51 | + |
| 52 | +### SDAM Events That Are Logged |
| 53 | + |
| 54 | +SDAM logging logs the following events which are raised in the course of monitoring the servers the driver is connected to. |
| 55 | + |
| 56 | +#### ClusterAddedServerEvent |
| 57 | + |
| 58 | +The [`ClusterAddedServerEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ClusterAddedServerEvent" >}}) is raised after a server has been added to the cluster. |
| 59 | + |
| 60 | +#### ClusterAddingServerEvent |
| 61 | + |
| 62 | +The [`ClusterAddingServerEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ClusterAddingServerEvent" >}}) is raised before a server is added to the cluster. |
| 63 | + |
| 64 | +#### ClusterClosedEvent |
| 65 | + |
| 66 | +The [`ClusterClosedEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ClusterClosedEvent" >}}) is raised after a cluster has been closed. |
| 67 | + |
| 68 | +#### ClusterClosingEvent |
| 69 | + |
| 70 | +The [`ClusterClosingEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ClusterClosingEvent" >}}) is raised before a cluster is closed. |
| 71 | + |
| 72 | +#### ClusterDescriptionChangedEvent |
| 73 | + |
| 74 | +The [`ClusterDescriptionChangedEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ClusterDescriptionChangedEvent" >}}) is raised when the cluster description changes. |
| 75 | + |
| 76 | +#### ClusterOpenedEvent |
| 77 | + |
| 78 | +The [`ClusterOpenedEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ClusterOpenedEvent" >}}) is raised after a cluster has been opened. |
| 79 | + |
| 80 | +#### ClusterOpeningEvent |
| 81 | + |
| 82 | +The [`ClusterOpeningEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ClusterOpeningEvent" >}}) is raised before a cluster is opened. |
| 83 | + |
| 84 | +#### ClusterRemovedServerEvent |
| 85 | + |
| 86 | +The [`ClusterRemovedServerEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ClusterRemovedServerEvent" >}}) is raised after a server has been removed from the cluster. |
| 87 | + |
| 88 | +#### ClusterRemovingServerEvent |
| 89 | + |
| 90 | +The [`ClusterRemovingServerEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ClusterRemovingServerEvent" >}}) is raised before a server is removed from the cluster. |
| 91 | + |
| 92 | +#### SdamInformationEvent |
| 93 | + |
| 94 | +The [`SdamInformationEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_SdamInformationEvent" >}}) is raised when something interesting happened that is not covered by a custom event type. |
| 95 | + |
| 96 | +#### ServerClosedEvent |
| 97 | + |
| 98 | +The [`ServerClosedEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ServerClosedEvent" >}}) is raised after a server has been closed. |
| 99 | + |
| 100 | +#### ServerClosingEvent |
| 101 | + |
| 102 | +The [`ServerClosingEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ServerClosingEvent" >}}) is raised before a server is closed. |
| 103 | + |
| 104 | +#### ServerDescriptionChangedEvent |
| 105 | + |
| 106 | +The [`ServerDescriptionChangedEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ServerDescriptionChangedEvent" >}}) is raised when the server description has changed. |
| 107 | + |
| 108 | +#### ServerHeartbeatFailedEvent |
| 109 | + |
| 110 | +The [`ServerHeartbeatFailedEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ServerHeartbeatFailedEvent" >}}) is raised after a heartbeat has failed. |
| 111 | + |
| 112 | +#### ServerHeartbeatStartedEvent |
| 113 | + |
| 114 | +The [`ServerHeartbeatStartedEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ServerHeartbeatStartedEvent" >}}) is raised after a heartbeat has started (but before the heartbeat is sent to the server). |
| 115 | + |
| 116 | +#### ServerHeartbeatSucceededEvent |
| 117 | + |
| 118 | +The [`ServerHeartbeatSucceededEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ServerHeartbeatSucceededEvent" >}}) is raised after a heartbeat has succeeded. |
| 119 | + |
| 120 | +#### ServerOpenedEvent |
| 121 | + |
| 122 | +The [`ServerOpenedEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ServerOpenedEvent" >}}) is raised after a server has been opened. |
| 123 | + |
| 124 | +#### ServerOpeningEvent |
| 125 | + |
| 126 | +The [`ServerOpeningEvent`]({{< apiref "T_MongoDB_Driver_Core_Events_ServerOpeningEvent" >}}) is raised before a server is opened. |
0 commit comments