I am not familiar with MongoDB, but as far as I can tell, there is no default support for it when it comes to Application Insights. But this does not mean that you cannot do this, it will just require a few more codes.
Again, I am not familiar with MongoDB, but according to http://www.mattburkedev.com/logging-queries-from-mongodb-c-number-driver/ , there is built-in support for logging generated requests. Now we only need to associate this with Application Insights.
Since you already know how to use TelemetryClient
, we can use our own tracking methods provided by this class. See https://docs.microsoft.com/nl-nl/azure/application-insights/app-insights-api-custom-events-metrics for available custom tracking methods.
All you have to do is insert code like this:
telemetryClient.TrackDependency( "MongoDB", // The name of the dependency query, // Text of the query DateTime.Now, // Time that query is executed TimeSpan.FromSeconds(0), // Time taken to execute query true); // Indicates success
The TelemetryClient
class is thread safe, so you can reuse it.
Now, according to the link blog, you can do something like this:
var client = new MongoClient(new MongoClientSettings() { Server = new MongoServerAddress("localhost"), ClusterConfigurator = cb => { cb.Subscribe<CommandStartedEvent>(e => { telemetryClient.TrackDependency( "MongoDB",
Again, I am not familiar with MongoDB, but I hope this is the starting point for your imagination on how to adapt it to your needs using your knowledge of MongoDB.
EDIT:
If there is also a CommandCompletedEvent
or similar event, unlike the CommandStartedEvent
event, you should probably track the dependency there, because then you will have to calculate (or read simpel) the time spent and possibly get the actual value for the success indicator.