首页 文章

WPF应用程序的应用程序见解

提问于
浏览
21

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

谢谢阿凡提

3 回答

  • 10

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

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

    在Azure中 - portal.azure.com

    • 创建应用程序资源 . ::新/开发者服务/应用程序见解 .

    • 注意生成的检测密钥,抓取副本并将其放在一边,我们在配置应用程序时需要它 .

    在您的申请中

    • NuGet - 添加'Application Insights API'

    • 配置 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);
    
    • 记录事件/屏幕/异常等

    任何时候'something happens'我将解决 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桌面应用程序的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 Package的0.17.0开始,如果您处于脱机状态,则刷新呼叫不会挂起,但会显示 . 在线,呼叫立即完成,离线在呼叫完成前有5秒暂停 .

  • 30

    虽然未列为受支持的应用类型,但这意味着没有收集/发送到应用程序洞察的默认遥测数据,也不支持添加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个遥测事件排队等待它们被发送 .

  • 7

    桌面应用程序的应用程序见解(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");
    

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

相关问题