2017-01-23 62 views
6

我有一個用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); 

回答

3

我不熟悉的MongoDB,但據當涉及到Application Insights時,我可以告訴它沒有默認的支持。但這並不意味着你不能這樣做,它只會涉及更多的代碼。

同樣,我不熟悉MongoDB,但根據http://www.mattburkedev.com/logging-queries-from-mongodb-c-number-driver/,內置支持記錄生成的查詢。現在,我們只需要將其與Application Insights掛鉤。

既然您已經知道如何使用TelemetryClient,我們可以使用該類提供的自定義跟蹤方法。有關可用的自定義追蹤方法,請參閱https://docs.microsoft.com/nl-nl/azure/application-insights/app-insights-api-custom-events-metrics

所有你需要做的是插入一些代碼:

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 

telemetryClient是線程安全的,所以你可以重複使用它。現在

,根據所引用的博客帖子,你應該能夠做這樣的事情:

var client = new MongoClient(new MongoClientSettings() 
{ 
    Server = new MongoServerAddress("localhost"), 
    ClusterConfigurator = cb => 
    { 
     cb.Subscribe<CommandStartedEvent>(e => 
     { 
      telemetryClient.TrackDependency(
       "MongoDB",    // The name of the dependency 
       e.Command.ToJson()  // Text of the query 
       DateTime.Now,   // Time that query is executed 
       TimeSpan.FromSeconds(0), // Time taken to execute query 
       true);     // Indicates success 
     }); 
    } 
}); 

同樣,我不熟悉的MongoDB,但我希望這是一個出發點爲你的想象力如何使用你對MongoDB的瞭解來適應你的需求。

編輯:

如果有也作爲反對CommandStartedEvent事件CommandCompletedEvent或類似的活動你應該跟蹤依賴關係,因爲你應該然後能計算出(或simpel閱讀)所花的時間,也許獲取成功指標的實際價值。

+3

然後如果你這樣做...那麼也許你應該爲它創建一個github回購並與世界分享它? :) –

+0

偉大的答案彼得,這不是真的那種在GitHub中工作的東西,但我可能會把我在做什麼的博客文章放在一起,我已經添加了代碼,我最終與我的問題。 –

+0

現在有點晚了,但我確實已經開始寫博客文章了:https://sequence7.net/2017/02/09/monitoring-mongodb-with-application-insights/ –