WPF 应用程序的应用程序洞察

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26505483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:38:28  来源:igfitidea点击:

Application Insights for WPF Application

wpfvisual-studioazure-devopsazure-application-insights

提问by Avanti Ajay

There is a WPF application written in Visual Studio. Can I add Application Insights to this WPF app? I would like to know how many times a button/tile is clicked. Since there are multiple installations of the same application, I would like to know which button was clicked how many times from which user/installation. Can this be done with Application Insights?

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

Thanks Avanti

谢谢阿凡提

回答by vladjoanovic

While not listed as a supported app type this means there isn't default telemetry data collected/sent to application insights nor is there support for adding AI/creating an application insights resource. That being said it is possible to add to your WPF with a few manual steps so that you can track the specific scenarios you mention (like a button/tile click).

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

-From Visual studio add the "Application Insights API" NuGet to the project (.11 is the latest today).

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

This will add the Application Insights API reference and create an application insights configuration file to your project.

这将添加 Application Insights API 参考并为您的项目创建应用程序洞察配置文件。

The applicationinsights.config file needs to be updated with your instrumentation key as follows:

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>

To create an application insights instrumentation key login to your azure subscription. https://portal.azure.comClick + to create an Application Insights resource. Then choose the properties tile on the application insights blade and copy the Instrumentation key and add it to your applicationinsights.config file. Now in your WPF app you can use the Application Insights sdk as described here: http://blogs.msdn.com/b/visualstudioalm/archive/2014/10/21/application-insights-sdk-0-11-0-prerelease.aspx

要创建应用程序洞察检测密钥,请登录到您的 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-预发布.aspx

your events will be visible in the diagnostic search blade which can be selected on the application insights blade.

您的事件将在诊断搜索刀片中可见,您可以在应用洞察刀片上选择该刀片。

Note: telemetry is batched locally for 1 min before being sent to the service unless > 500 telemetry events are queued at which point they are sent.

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

回答by ben

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

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

An official link from Microsoft on how to add Application Insights to a Windows Forms application. From the link:

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

In Azure - portal.azure.com

在 Azure 中 - portal.azure.com

  1. Create an Application Resource. ::New / Developer Services / Application Insights.
  2. Notice the instrumentation key generated, grab a copy and set it aside, we'll need it when we configure your application.
  1. 创建应用程序资源。::新/开发者服务/应用洞察。
  2. 注意生成的检测密钥,抓取一个副本并将其放在一边,我们在配置您的应用程序时需要它。

In Your Application

在您的应用程序中

  1. NuGet - Add 'Application Insights API'
  2. Configure your TelemetryClient.
  1. NuGet - 添加“应用程序洞察 API”
  2. 配置您的TelemetryClient.

I'm using MvvmCross in a WPF application, on startup I create a single TelemetryClientthat I re-use throughout the application.

我在 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. Record an event/screen/exception, etc
  1. 记录事件/屏幕/异常等

Any time 'something happens' I'll resolve the TelemetryClientand record the event. This is just like any other Application Insights implementation with regards to tracking and recording.

任何时候“有事发生”,我都会解决TelemetryClient并记录事件。这与跟踪和记录方面的任何其他 Application Insights 实现一样。

As an example -

举个例子 -

//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. Flush on Application Exit
  1. 应用程序退出时刷新

Application Insights for Windows Desktop applications does not automatically gather/send anything. As a developer one needs to force a flush at application exit.

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

Or setup an RxTimer to flush on a schedule...I decided to flush every 30 minutes:

或者设置一个 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");
    }
})));

FYI - As of 0.17.0 of the Application Insights API NuGet Package if you are offline the flush call doesn't hang, but appears to. Online, the call completes immediately, offline there is a solid 5 second pause before the call completes.

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

回答by Ohad Schneider

Application Insights (AI) for desktop applications is being deprecated in favor of HockeyApp. It's not overly mature yet, but it works (events essentially reach the same place AI events go).

桌面应用程序的 Application Insights (AI) 已被弃用,取而代之的是HockeyApp。它还不是太成熟,但它有效(事件基本上到达了 AI 事件去的同一个地方)。

For example, here's how it looks in RoslynPad(a WPF C# Editor):

例如,这是它在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");

EDITLooks like the following NuGet package is required in order for this to work properly: https://www.nuget.org/packages/HockeySDK.WPF.TelemetryWorkaround(see https://github.com/bitstadium/HockeySDK-Windows/pull/88).

编辑看起来需要以下 NuGet 包才能使其正常工作:https: //www.nuget.org/packages/HockeySDK.WPF.TelemetryWorkaround(参见https://github.com/bitstadium/HockeySDK-Windows/拉/88)。