【问题标题】:Application Insights for WPF ApplicationWPF 应用程序的 Application Insights
【发布时间】:2017-01-26 23:39:38
【问题描述】:

有一个用 Visual Studio 编写的 WPF 应用程序。 我可以将 Application Insights 添加到此 WPF 应用程序吗? 我想知道一个按钮/图块被点击了多少次。由于有多个安装 对于同一个应用程序,我想知道哪个按钮从哪个用户/安装中单击了多少次。这可以通过 Application Insights 完成吗?

谢谢 阿凡提

【问题讨论】:

标签: wpf visual-studio azure-devops azure-application-insights


【解决方案1】:

虽然未列为受支持的应用类型,但这意味着没有收集/发送到应用洞察的默认遥测数据,也不支持添加 AI/创建应用洞察资源。话虽如此,可以通过几个手动步骤将其添加到您的 WPF 中,以便您可以跟踪您提到的特定场景(例如按钮/磁贴单击)。

-从 Visual Studio 将“Application Insights API”NuGet 添加到项目中(.11 是今天最新的)。

这将添加 Application Insights 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 单击 + 以创建 Application Insights 资源。 然后选择应用程序洞察刀片上的属性磁贴,复制 Instrumentation 密钥并将其添加到您的 applicationinsights.config 文件中。 现在,在您的 WPF 应用程序中,您可以使用 Application Insights sdk,如下所述:http://blogs.msdn.com/b/visualstudioalm/archive/2014/10/21/application-insights-sdk-0-11-0-prerelease.aspx

您的事件将显示在诊断搜索刀片中,可以在应用程序洞察刀片上进行选择。

注意:遥测在发送到服务之前会在本地批处理 1 分钟,除非在发送时排队超过 500 个遥测事件。

【讨论】:

  • 应用关闭时的批处理事件呢?异常是批处理还是立即发送?
  • 感谢您的回答,它让我足够了解我需要知道的其余部分。
【解决方案2】:

https://azure.microsoft.com/en-us/documentation/articles/app-insights-windows-desktop/

Microsoft 提供的有关如何将 Application Insights 添加到 Windows 窗体应用程序的官方链接。从链接:

在 Azure 中 - portal.azure.com

  1. 创建应用程序资源。 ::新/开发人员服务/应用程序洞察力。
  2. 注意生成的检测密钥,获取一份副本并将其放在一边,我们在配置您的应用程序时将需要它。

在您的应用程序中

  1. NuGet - 添加“Application Insights API”
  2. 配置您的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);
  1. 记录事件/屏幕/异常等

任何时候“发生某事”,我都会解决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);
}
  1. 应用程序退出时刷新

适用于 Windows 桌面应用程序的 Application Insights 不会自动收集/发送任何内容。作为开发人员,需要在应用程序退出时强制刷新。

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");
    }
})));

仅供参考 - 从 Application Insights API NuGet 包的 0.17.0 开始,如果您处于脱机状态,则刷新调用不会挂起,但似乎会挂起。在线时,通话立即完成,离线时,通话完成前会停顿 5 秒。

【讨论】:

  • 您关于 AI 不会自动发送事件的注释与已接受的答案冲突(由显然在 AI 团队工作的 MS 员工撰写):遥测数据在本地批处理 1 分钟,然后发送到服务,除非 > 500 个遥测事件在发送时排队。所以我猜你设置的计时器是不必要的,在应用程序退出时刷新应该就足够了。
  • @Vadim 你能帮我在 Prism 上注册吗?
【解决方案3】:

桌面应用程序的 Application Insights (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)。

【讨论】:

  • @MicheleFerracin 你仍然可以使用这个包,只是它没有出现在搜索结果中。作者删除了它,因为它是一个 hacky 解决方案,他转而直接在 RP 中使用 AI(您可以在 GitHub 上查看他的代码或查看上面的答案)。
猜你喜欢
  • 2020-08-02
  • 1970-01-01
  • 2020-02-02
  • 1970-01-01
  • 1970-01-01
  • 2023-02-09
  • 1970-01-01
  • 2019-08-02
  • 2022-01-11
相关资源
最近更新 更多