Learn about RadDiagnostics for retrieving Exception Details using the Telerik Controls for WP7


Telerik introduces a new component called “RadDiagnostics” in their Q1.2012 release. RadDiagnostics component enables an application developer to receive crash reports and other debugging information from the end user of the application.

 

As a WP7Dev, you may want to include a functionality in your application which will mail you the stack trace and other debugging information. This post will guide you to implement the same functionality in your Windows Phone app using the Telerik controls. Continue reading to learn more about it.

 

Know About the API

When our Windows Phone application crashes at runtime and generates exception due to unhandled cases, this is needed by the app developer to know about the exception details with StackTrace so that the developer can debug that piece of code and fix the issue.

 

Note: You can skip to the next section to learn about the integration

 

Telerik RadDiagnostics component allows you to integrate an unhandled exception handler which will allow the user of the phone application to send error details to the developer through mail.

 

RadDiagnostics class is present in the Telerik.Windows.Controls namespace and exposes few properties to the developer to add error details to the object. Here is the meta data of the class:

 

 
namespace Telerik.Windows.Controls
{
    public class RadDiagnostics
    {
        public RadDiagnostics();
 
        public string EmailTo { get; set; }
        public string EmailSubject { get; set; }
        public string ApplicationVersion { get; set; }
        public string ApplicationName { get; set; }
        public MessageBoxInfoModel MessageBoxInfo { get; set; }
        public bool HandleUnhandledException { get; set; }
        public bool IncludeScreenshot { get; set; }
        public string DiagnosticInfo { get; }
 
        public static void AddDebugInfo(string info);
        public static void AddDebugInfo(string info, bool printInOutputWindow);
 
        public void Init();
        public void ReportHandledException(Exception e);
 
        protected virtual void AddDiagnosticLine(StringBuilder builder, 
                                                    object paramName, object paramValue);
        public event EventHandler<ExceptionOccurredEventArgs> ExceptionOccurred;
    }
}

 

 

There is a private method called SendErrorReport() inside the RadDiagnostics class which composes the email message and calls the Show() method to launch the email client to send the email message.

 

Here is the decompiled version of the method which you can study and understand how this has been implemented:

 
private void SendErrorReport(ExceptionOccurredEventArgs eventArgs)
{
  EmailComposeTask emailComposeTask = new EmailComposeTask();
  emailComposeTask.To = this.EmailTo;
  emailComposeTask.Subject = string.Format(this.EmailSubject, 
                                   (object) this.ApplicationName, 
                                   (object) this.ApplicationVersion, 
                                   (object) eventArgs.Exception.StackTrace.GetHashCode());
  emailComposeTask.Body = this.ComposeMessageBody(eventArgs);
  try
  {
    emailComposeTask.Show();
  }
  catch (ArgumentOutOfRangeException ex)
  {
    string str = emailComposeTask.Body.Replace(this.imageString, 
                                       "Image Skipped (too big for EmailComposeTask)");
    emailComposeTask.Body = str;
    emailComposeTask.Show();
  }
}

 

To know more about the emailing APIs of Windows Phone 7, read the following blog posts where I detailed about the API and the implementation steps:

 

So, what are the information's that gets inserted into the email message? Ok, Telerik component internally handles those using the private method named ComposeDiagnosticsInfo() which returns a string of message to the caller. I hope the below decompiled code will give you better visibility of the information that gets inserted into the message:

 

 
private string ComposeDiagnosticInfo(Exception e)
{
  StringBuilder builder = new StringBuilder();
  this.AddDiagnosticLine(builder, (object) "ExceptionMessage", 
                        (object) e.Message);
  this.AddDiagnosticLine(builder, (object) "StackTrace", 
                        (object) (System.Environment.NewLine + e.StackTrace));
  this.AddDiagnosticLine(builder, (object) "OccurrenceDate", 
                        (object) DateTime.Now.ToUniversalTime().ToString("r"));
  this.AddDiagnosticLine(builder, (object) "AppInstallDate", 
        (object) ApplicationUsageHelper.InitialInstallationDate.ToUniversalTime().ToString("r"));
  this.AddDiagnosticLine(builder, (object) "AppTotalRuns", 
                        (object) ApplicationUsageHelper.ApplicationRunsCountTotal);
  this.AddDiagnosticLine(builder, (object) "AppRunsAfterLastUpdate", 
                        (object) ApplicationUsageHelper.ApplicationRunsCountForCurrentVersion);
  if (ApplicationUsageHelper.PreviousRunDate.HasValue)
    this.AddDiagnosticLine(builder, (object) "AppPreviousRunDate", 
        (object) ApplicationUsageHelper.PreviousRunDate.Value.ToUniversalTime().ToString("r"));
  this.AddDiagnosticLine(builder, (object) "AppVersion", (object) this.ApplicationVersion);
  this.AddDiagnosticLine(builder, (object) "Culture", (object) CultureInfo.CurrentCulture);
  if (Application.Current.RootVisual != null)
  {
    PhoneApplicationFrame rootFrame = Application.Current.RootVisual as PhoneApplicationFrame;
    if (rootFrame != null && rootFrame.CurrentSource != (Uri) null)
    {
      this.AddDiagnosticLine(builder, (object) "CurrentPageSource", 
                            (object) rootFrame.CurrentSource.ToString());
      this.AddDiagnosticLine(builder, (object) "NavigationStack", 
                            (object) this.GetNavigationStackInfo(rootFrame));
    }
  }
  this.AddDiagnosticLine(builder, (object) "DeviceManufacturer", 
                        (object) DeviceStatus.DeviceManufacturer);
  this.AddDiagnosticLine(builder, (object) "DeviceModel", 
                        (object) DeviceStatus.DeviceName);
  this.AddDiagnosticLine(builder, (object) "DeviceHardwareVersion", 
                        (object) DeviceStatus.DeviceHardwareVersion);
  this.AddDiagnosticLine(builder, (object) "DeviceFirmwareVersion", 
                        (object) DeviceStatus.DeviceFirmwareVersion);
  this.AddDiagnosticLine(builder, (object) "OSVersion", 
                        (object) System.Environment.OSVersion);
  this.AddDiagnosticLine(builder, (object) "CLRVersion", 
                        (object) System.Environment.Version);
  this.AddDiagnosticLine(builder, (object) "DeviceType", 
                        (object) Microsoft.Devices.Environment.DeviceType);
  this.AddDiagnosticLine(builder, (object) "NetworkType", 
                        (object) NetworkInterface.NetworkInterfaceType);
  this.AddDiagnosticLine(builder, (object) "DeviceTotalMemory(Mb)", 
                        (object) DeviceExtendedPropertiesHelper.GetTotalMemoryMb());
  this.AddDiagnosticLine(builder, (object) "AppPeakMemoryUsage(Mb)", 
                        (object) DeviceExtendedPropertiesHelper.GetPeakMemoryUsageMb());
  this.AddDiagnosticLine(builder, (object) "AppCurrentMemoryUsage(Mb)", 
                        (object) DeviceExtendedPropertiesHelper.GetCurrentMemoryUsageMb());
  .
  .
  .
  .
  .
  if (RadDiagnostics.debugInfo.Length > 0)
  {
    builder.AppendFormat(this.diagnosticLine, new object[2]
    {
      (object) "**Debug Data**",
      (object) ((object) RadDiagnostics.debugInfo).ToString()
    });
    builder.AppendLine();
  }
  return ((object) builder).ToString();
}

 

Hope the above decompiled version was helpful for you to understand the internal implementation of the Telerik component. In the next step, we will learn how to integrate it in our application.

FacebookTwitterRSS feedsEmail Newsletter

How to Implement it?

Telerik RadDiagnostics Component: Required AssembliesNow it’s time for you to integrate this in your Windows Phone application. At the first step, you have to include the following DLL reference in your project:

                • Telerik.Windows.Controls.Primitives
                • Telerik.Windows.Core

For simplicity of this Demo, we will create a button in the MainPage.xaml file and register the button click event. In the button click event, we will throw an Exception from the application. In general this step is not require.

 

Now open your App.xaml.cs file and navigate to the constructor. At the end of the constructor implementation, add the following code snippet:

 
var radDiagnostics = new RadDiagnostics
                         {
                             EmailTo = EMAIL_ADDRESS_OF_APP_DEVELOPER, 
                             IncludeScreenshot = true, 
                             HandleUnhandledException = true
                         };
radDiagnostics.Init();

 

You can add/modify any properties during the object construction in this place. Don’t forget to add the above fields here. Now, navigate to the “Application_Launching” event and add the below code there:

 
ApplicationUsageHelper.Init("1.0");

 

ApplicationUsageHelper class is present in the Telerik.Windows.Core assembly and provides data about the application usages. Don’t forget to add this line in the Application_Launching event; otherwise the RadDiagnostics component will not work.

 

The button click event implementation will have the following line to throw an exception explicitly from the demo application to test it:

 
throw new Exception("Explicitly throwing Exception to test");

 

imageNow when the application throws exception, it will show a message box in the screen asking user to notify the application developer about this error with complete error details by mail.

 

If the user clicks “yes” to proceed, it will compose the mail and launch the email client in the UI. Clicking “no” will bring the user to the caller page.

 

This way you can help the end user to notify any errors happened in your application to you so that, you can take proper steps to rectify the issue and publish a patch to the market place.

 

End Note

I hope that, this article was useful to you to understand about the API and the usages of it in your Windows Phone 7 application. Now you will be able to use it very easily in your apps.

 

If you have any queries, please use the comment form below to leave your queries. I will try to answer you as soon as possible. Your feedback and suggestions are always welcome and highly appreciable.

 

Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

1 comments

  1. Thanks for the link, I’ve never seen that site before. Very interesting

    ReplyDelete

 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top