我有一个用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/ –