我有一個用C#編寫的控制檯應用程序項目,我已將Application Insights添加到以下NuGet包中。如何跟蹤控制檯應用程序中的MongoDB請求
Microsoft.ApplicationInsights
Microsoft.ApplicationInsights.Agent.Intercept
Microsoft.ApplicationInsights.DependencyCollector
Microsoft.ApplicationInsights.NLogTarget
Microsoft.ApplicationInsights.PerfCounterCollector
Microsoft.ApplicationInsights.Web
Microsoft.ApplicationInsights.WindowsServer
Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel
我已經在配置文件中配置我InstrumentationKey和我使用下面的代碼發射了TelemetryClient在啓動時:除了AI
var telemetryClient = new TelemetryClient();
telemetryClient.Context.User.Id = Environment.UserName;
telemetryClient.Context.Session.Id = Guid.NewGuid().ToString();
telemetryClient.Context.Device.OperatingSystem = Environment.OSVersion.ToString();
一切工作以及沒有捕捉任何發送到Mongo的請求,我都可以看到請求在「應用程序映射」中轉到SQL服務器,但沒有任何其他外部請求的跡象。有什麼方法可以看到對Mongo提出的請求的遙測?
編輯 - 感謝彼得絲寶我結束了幾乎它就像一個魅力,讓我成功與失敗之間的區別如下:
var telemetryClient = new TelemetryClient();
var connectionString = connectionStringSettings.ConnectionString;
var mongoUrl = new MongoUrl(connectionString);
var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl);
mongoClientSettings.ClusterConfigurator = clusterConfigurator =>
{
clusterConfigurator.Subscribe<CommandSucceededEvent>(e =>
{
telemetryClient.TrackDependency("MongoDB", e.CommandName, DateTime.Now.Subtract(e.Duration), e.Duration, true);
});
clusterConfigurator.Subscribe<CommandFailedEvent>(e =>
{
telemetryClient.TrackDependency("MongoDB", $"{e.CommandName} - {e.ToString()}", DateTime.Now.Subtract(e.Duration), e.Duration, false);
});
};
var mongoClient = new MongoClient(mongoClientSettings);
然後如果你這樣做...那麼也許你應該爲它創建一個github回購並與世界分享它? :) –
偉大的答案彼得,這不是真的那種在GitHub中工作的東西,但我可能會把我在做什麼的博客文章放在一起,我已經添加了代碼,我最終與我的問題。 –
現在有點晚了,但我確實已經開始寫博客文章了:https://sequence7.net/2017/02/09/monitoring-mongodb-with-application-insights/ –