Tracking exceptions and events

Now that we have an IAnalyticsService property in all of our ViewModels, we can update all of our try/catch blocks to pass exceptions to App Center. For example, in MainViewModel, we have a try/finally block in the LoadEntries method that is currently not catching exceptions.

Update this try/finally block with a catch block and then pass any caught Exception off to the analytics service via the TrackError method:

void LoadEntries()
{
if (IsBusy)
{
return;
}

IsBusy = true;

try
{
// ...
}
catch (Exception e)
{
AnalyticsService.TrackError(e,
new Dictionary<string, string>
{
{"Method", "MainViewModel.LoadEntries()"}
});

}
finally
{
IsBusy = false;
}
}

The App Center Crashes SDK automatically reports all unhandled exceptions once it is enabled in the app.

We can also start tracking user events throughout the application. For example, if we wanted to know how often users viewed the entry detail page in our app, we could call the TrackEvent method of IAnalyticsService within the Init method of DetailViewModel to log that in App Center Analytics:

public class DetailViewModel : BaseViewModel<TripLogEntry>
{
// ...

public override async Task Init(TripLogEntry logEntry)
{
AnalyticsService.TrackEvent("Entry Detail Page",
new Dictionary<string, string>
{
{ "Title", logEntry.Title }
});

// ...

}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.50.87