{"id":6241,"date":"2018-05-13T20:46:48","date_gmt":"2018-05-13T20:46:48","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6241"},"modified":"2019-01-03T17:10:22","modified_gmt":"2019-01-03T17:10:22","slug":"monitoring-your-application-using-app-center","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/monitoring-your-application-using-app-center\/","title":{"rendered":"Monitoring your application using App Center"},"content":{"rendered":"<p>Microsoft&#8217;s App Center (https:\/\/appcenter.ms\/apps) allows us to do several things, including build and distribution of our application(s), but what I&#8217;m interested in for this post, is the capabilities which allow us to monitor some usage information, tracing events and viewing exceptions.<\/p>\n<p>Within App Center you need to create an application and tell App Center what OS and Platform is being used and it will respond by supplying you with information on how to &#8220;embed&#8221; App Center code into your application.<\/p>\n<p>Once we&#8217;ve created an application in App Center we&#8217;ll be supplied with a key (currently you need a key per OS application).<\/p>\n<p>Let&#8217;s now create a simply test application using Xamarin.Forms, although you can create a standard UWP application if you prefer.<\/p>\n<p><em>Note: at this time WPF does not seem to be fully supported.<\/em><\/p>\n<p><strong>Creating our Xamarin.Forms application<\/strong><\/p>\n<ul>\n<li>In Visual Studio, select Mobile App (Xamarin.Forms) project, this will allow us to create a UWP, IOS and Android application (ensure all three platforms are selected).<\/li>\n<li>Now add the two NuGet packages, Microsoft.AppCenter.Analytics and Microsoft.AppCenter.Crashes to each platform specific project.<\/li>\n<\/ul>\n<p>Inside the shared project, edit the App.xaml.cs adding the following using clauses<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing Microsoft.AppCenter;\r\nusing Microsoft.AppCenter.Analytics;\r\n<\/pre>\n<p>and within the OnStart, add the following code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nAppCenter.Start(\r\n   &quot;android=ANDROID_KEY;&quot; +\r\n   &quot;uwp=UWP_KEY;&quot; +\r\n   &quot;ios=IOS_KEY&quot;,\r\n   typeof(Analytics), \r\n   typeof(Crashes));\r\n<\/pre>\n<p>For each platform you need to create a new application key within App Center. If you are only intending to create a single platform version of your application, for example a UWP application, you can use the following instead<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nAppCenter.Start(UWP_KEY,\r\n   typeof(Analytics), \r\n   typeof(Crashes));\r\n<\/pre>\n<p>If we now run our application, App Center will start to log each time the application is run along with information regarding the OS being used, but what if we want to start to track certain usage, for example to find out if a feature is actually being used.<\/p>\n<p><strong>Exceptions and crash reporting<\/strong><\/p>\n<p>Crash\/Exception reporting is enabled by default but obviously if you&#8217;re running inside the Debugger in Visual Studio, this may intercept such exceptions before they get sent to the App Center, so if your using a mobile device, you&#8217;ll need to actually deploy the application to test any exception handling.<\/p>\n<p>However in instances where we want to both handle an exception (probably most cases) and log\/track the exception we use code like the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ntry\r\n{\r\n   \/\/ do something\r\n}\r\ncatch(Exception e) \r\n{\r\n   Crashes.TrackError(e);\r\n}\r\n<\/pre>\n<p>We can also supply a dictionary with additional information to TrackError, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ntry\r\n{\r\n   \/\/ do something\r\n}\r\ncatch(Exception e) \r\n{\r\n   Crashes.TrackError(e, new Dictionary&lt;string, string&gt;\r\n   {\r\n      { &quot;WithFile&quot;, filename }\r\n   });\r\n}\r\n<\/pre>\n<p><strong>Tracking events<\/strong><\/p>\n<p>To track events, simple add the following line (obviously replacing the text with whatever you want to see in App Center when an event occurs).<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nAnalytics.TrackEvent(&quot;New Feature Used&quot;);\r\n<\/pre>\n<p>we can also supply more information to the event tracking, by supplying a dictionary to the TrackEvent method, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nAnalytics.TrackEvent(&quot;New Feature Used&quot;, new Dictionary&lt;string, string&gt;\r\n{\r\n   { &quot;WithFile&quot;, filename }\r\n});\r\n<\/pre>\n<p><strong>Turning Analytics on\/off<\/strong><\/p>\n<p>Obviously you might want to have a configuration option which allows a user to opt-in\/out of analytics, simply use<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nAnalytics.SetEnabledAsync(false);\r\n<\/pre>\n<p><strong>Offline behaviour<\/strong><\/p>\n<p>Obviously all this is great if the application\/device in connected to the internet but in situations where the application or device are offline, messages will be stored locally. The <a href=\"https:\/\/docs.microsoft.com\/en-us\/appcenter\/analytics\/faq\" rel=\"noopener\" target=\"_blank\">FAQ<\/a> states that up to 300 logs can be stored offline and once a internet connection is restored the messages will be sent to the App Center. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Microsoft&#8217;s App Center (https:\/\/appcenter.ms\/apps) allows us to do several things, including build and distribution of our application(s), but what I&#8217;m interested in for this post, is the capabilities which allow us to monitor some usage information, tracing events and viewing exceptions. Within App Center you need to create an application and tell App Center what [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[217,106,122],"tags":[],"class_list":["post-6241","post","type-post","status-publish","format-standard","hentry","category-app-center","category-universal-app","category-xamarin-forms"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=6241"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6241\/revisions"}],"predecessor-version":[{"id":6684,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6241\/revisions\/6684"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}