有一个使用Visual Studio编写的WPF应用程序。 我能将Application Insights添加到这个WPF应用程序中吗? 我想知道按钮/磁贴被点击了多少次。由于同一应用程序有多个安装实例, 我想知道来自哪个用户/安装的按钮被点击了多少次。这可以使用Application Insights完成吗?
谢谢 Avanti
有一个使用Visual Studio编写的WPF应用程序。 我能将Application Insights添加到这个WPF应用程序中吗? 我想知道按钮/磁贴被点击了多少次。由于同一应用程序有多个安装实例, 我想知道来自哪个用户/安装的按钮被点击了多少次。这可以使用Application Insights完成吗?
谢谢 Avanti
虽然不被列为受支持的应用程序类型,这意味着没有默认的遥测数据被收集/发送到应用程序洞察力,也没有支持添加AI /创建应用程序洞察资源。 话虽如此,您仍可以通过几个手动步骤将其添加到您的WPF,以便您可以跟踪您提到的特定情况(例如按钮/瓦片点击)。
-从Visual Studio向项目添加“Application Insights API” NuGet(.11是今天最新的)。
这将添加应用程序洞察API引用并创建一个应用程序洞察配置文件到您的项目中。
需要更新applicationinsights.config文件,具体如下:
<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings" schemaVersion="2014-05-30">
<TelemetryChannel>
<DeveloperMode>false</DeveloperMode>
</TelemetryChannel>
<TelemetryModules>
<Add Type="Microsoft.ApplicationInsights.Tracing.DiagnosticsTelemetryModule, Microsoft.ApplicationInsights"/>
</TelemetryModules>
<InstrumentationKey>**your-instrumentation-key-guid**</InstrumentationKey>
</ApplicationInsights>
创建应用程序洞察仪器密钥,请登录到您的Azure订阅。 https://portal.azure.com 单击“+”以创建一个应用程序洞察资源。 然后,在应用程序洞察刀片上选择属性磁贴,并复制仪表板密钥并将其添加到您的applicationinsights.config文件中。 现在,在您的WPF应用程序中,您可以使用此处描述的Application Insights SDK:http://blogs.msdn.com/b/visualstudioalm/archive/2014/10/21/application-insights-sdk-0-11-0-prerelease.aspx
您的事件将在诊断搜索刀片中可见,该刀片可以在应用程序洞察刀片上选择。
注意:除非排队的遥测事件> 500个,否则本地会将遥测批处理1分钟后发送到服务。
https://azure.microsoft.com/zh-cn/documentation/articles/app-insights-windows-desktop/
这是微软官方提供的链接,介绍如何将应用程序洞察添加到Windows Forms应用程序中。链接内容如下:
Azure - portal.azure.com
在您的应用程序中
TelemetryClient
。我在WPF应用程序中使用MvvmCross,在启动时创建一个单一的 TelemetryClient
,并在整个应用程序中重复使用。
var telemetryClient = new TelemetryClient();
telemetryClient.InstrumentationKey = "your key here from Azure";
telemetryClient.Context.Session.Id = Guid.NewGuid().ToString();
telemetryClient.Context.User.AccountId = Username;
telemetryClient.Context.Component.Version = Settings.Default.Version;
telemetryClient.TrackEvent("Application Start");
Mvx.RegisterSingleton<TelemetryClient>(telemetryClient);
每当有“某事发生”时,我会调用TelemetryClient
并记录事件。这与其他关于跟踪和记录的Application Insights实现类似。
例如 -
//Resolve the telemetry client
readonly TelemetryClient telemetryClient = Mvx.Resolve<TelemetryClient>();
//Record a page View with some extra information
var pageviewTelemetry = new PageViewTelemetry("Observations");
pageviewTelemetry.Properties.Add("Breadcrumb", breadcrumb);
telemetryClient.TrackPageView(pageviewTelemetry);
//Track an event
var eventTelemetry = new EventTelemetry("Observation Saved");
eventTelemetry.Properties.Add("Saved Observation", observation);
telemetryClient.TrackEvent(eventTelemetry);
//Track an exception
try
{
// do work here
}
catch (Exception ex)
{
telemeteryClient.TrackException(ex);
}
Windows桌面应用程序的应用洞察不会自动收集/发送任何内容。 作为开发人员,需要在应用程序退出时强制执行刷新操作。
private void PowerButton_OnClick(object sender, RoutedEventArgs e)
{
var tc = Mvx.Resolve<TelemetryClient>();
if (null != tc)
{
tc.Flush(); // only for desktop apps
}
Application.Current.Shutdown();
}
或者设置一个RxTimer按时间表刷新...我决定每30分钟刷新一次:
var observable = Observable.Interval(new TimeSpan(0, 0, 30, 0));
observable.Subscribe(_ => Application.Current.Dispatcher.Invoke(new Action(() =>
{
var tc = Mvx.Resolve<TelemetryClient>();
if (null != tc)
{
tc.Flush(); // only for desktop apps
Console.WriteLine("Flush TC");
}
})));
注意 - 从应用程序洞察API NuGet包0.17.0版开始,如果您处于离线状态,则刷新调用不会挂起,但表面上会出现挂起的状态。在线时,调用立即完成,而离线时,调用完成前需要等待5秒钟。
桌面应用程序的应用洞察(AI)正在被淘汰,转而使用HockeyApp。它还不是非常成熟,但它可以工作(事件基本上到达与 AI 事件相同的位置)。
例如,这是在RoslynPad(一个 WPF C# 编辑器)中的样子:
using Microsoft.HockeyApp;
//In your initialization method:
var hockeyClient = (HockeyClient)HockeyClient.Current;
hockeyClient.Configure(HockeyAppId)
.RegisterCustomDispatcherUnhandledExceptionLogic(OnUnhandledDispatcherException)
.UnregisterDefaultUnobservedTaskExceptionHandler();
var platformHelper = (HockeyPlatformHelperWPF)hockeyClient.PlatformHelper;
platformHelper.AppVersion = _currentVersion.ToString();
hockeyClient.TrackEvent("App Start");
//sometime later:
hockeyClient.TrackEvent("Something happened");
编辑 看起来需要安装以下 NuGet 包才能正常工作: https://www.nuget.org/packages/HockeySDK.WPF.TelemetryWorkaround (参见 https://github.com/bitstadium/HockeySDK-Windows/pull/88)。