[] UWP use Exceptionless telemetry in the UWP

A few weeks ago on the company's test server set up Exceptionless the company Asp.net core projects telemetry. Before the Remote Desktop login is up, then look through the log file to see if there is no abnormality, the efficiency is extremely low. After using Exceptionless, a lot of high efficiency, but also to collect more information on the.

Recently prepared to the New Year, the project is also on the line for some time to stabilize, as a relatively busy little. UWP items on the handle or else thinking also add Exceptionless telemetry, after all, Microsoft Store Dashboard that anomalies are almost can not see what, and this test server performance is excess, not white do not.

But when I operate, they found a big problem, Exceptionless official did not provide UWP library (Winform, WPF, asp.net core have). I checked the relevant issue, the official suggested on github with .net standard version. However, after practice, I found that require additional code to handle the UWP in UnhandledException, paper record it.

 

The first is to create a project on Exceptionless get ApiKey platform, this is not to say. Then UWP project reference Exceptionless of nuget package .

Add SetUpExceptionless App.xaml.cs code in the constructor of:

public App()
{
    this.InitializeComponent();
    this.Suspending += OnSuspending;
    SetUpExceptionless();
}

Then the first preliminary implementation SetUpExceptionless method:

private void SetUpExceptionless()
{
    var client = ExceptionlessClient.Default;
    client.Configuration.ServerUrl = "";
    client.Configuration.ApiKey = "";

    client.Startup();
}

Because it is self Exceptionless platform, so you need to configure it ServerUrl. ApiKey configured to get from the platform to the ApiKey.

Next is the process UnhandledException, the UWP platform, there are two cases, one is triggered on the App class, the other is the trigger on the AppDomain. We have to listen to these two. But how to monitor it, take a look at where the official in WPF platform is how to achieve:

Snipaste_2020-01-07_12-09-59

FIG mainly two rows on the red line, the first line of the main thread exception is captured, the second line of the captured exception App. The internal capture thread unusual method uses Winform class, where we ignore, because there are no classes UWP, UWP and Central Africa is abnormal UI thread can be caught by AppDomain.UnhandledException event. Here we see the realization of the second line :

Snipaste_2020-01-07_12-15-04

可见是对 System.Windows.Application.Current.DispatcherUnhandledException 事件进行了订阅,然后给 Exceptionless。

依样画葫芦,完善我们的 SetUpExceptionless 方法:

private void SetUpExceptionless()
{
    var client = ExceptionlessClient.Default;
    client.Configuration.ServerUrl = "";
    client.Configuration.ApiKey = "";

    UnhandledException += (sender, args) =>
    {
        var contextData = new ContextData();
        contextData.MarkAsUnhandledError();
        contextData.SetSubmissionMethod("App_UnhandledException");

        args.Exception.ToExceptionless(contextData, ExceptionlessClient.Default).Submit();

        client.ProcessQueue();
    };
    AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
    {
        var exception = args.ExceptionObject as Exception;
        if (exception == null)
        {
            return;
        }

        var contextData = new ContextData();
        contextData.MarkAsUnhandledError();
        contextData.SetSubmissionMethod("AppDomain_UnhandledException");

        exception.ToExceptionless(contextData, ExceptionlessClient.Default).Submit();

        client.ProcessQueue();
    };

    client.Startup();
}

Next, we test whether the onset, first manually throw exceptions, I'll just build a Button, and then Click OK to throw exceptions.

private void TestButton_Click(object sender, RoutedEventArgs e)
{
    throw new Exception("测试 Exceptionless 集成");
}

After the operation, click the button, and then after we see Exceptionless App blow up the platform.

Snipaste_2020-01-07_12-30-13

perfect!

Guess you like

Origin www.cnblogs.com/h82258652/p/12160847.html
UWP
UWP
UWP
UWP
UWP
UWP
UWP