Click here to Skip to main content
15,884,537 members
Articles / General Programming / Exceptions

WinRT Apps Exception Logging

Rate me:
Please Sign up or sign in to vote.
4.80/5 (8 votes)
2 Dec 2013CPOL4 min read 20.6K   6   3
WinRT Apps exception logging with WinrtErrLog and Google Spreadsheets.

Introduction

WinrtErrLog is a small C# library that helps the app to post the exception details of Google Form and save it to Google Spreadsheet.

Background

SMTP needs to access each and every location in the hard drive in order to export the log to a specific folder so that the user can email the log to the owner. Since, WinRT runs in Sandbox mode, it is unable to access each and every folder in the hard drive, thus this option is not feasible for exception handling.

Also, many paid alternatives are available which offer the exception log in an intuitive web based dashboard form, e.g., MarkedUp, Raygun, BugSense, etc. With these paid solutions, one needs to create a web service which can store the exception logs in database, use an IIS enabled web server to host web service and a MS-SQL database is also required.

Logging with Google Forms

Google Forms is primarily a tool that is used in high volumes for surveys, event planning, and easy collection of information. It can be easily connected to Google Spreadsheet and responses will be automatically sent to Google Spreadsheet.

Google Forms is a good solution to save exception details, where Google Spreadsheets can be used to view exception logs. It is available for free.

How to create Google Form?

  • Sign in with Gmail account and open Google drive.
  • Click on the “Create” button on the left side.
  • Select “Form” from the dropdown menu.

Image 1

Google forms offers a wide range of options to create a form, but for this only four fields are required which will act as four columns.

  • Exception Type
  • Exception Message
  • Exception Stack Trace
  • Exception Source

With all four text items and appropriate question titles, the final form should look like this:

Image 2

After creating the form, the next step would be to create a spreadsheet and to link it with the form created. In this case "My XYZ App Exception Log".

Image 3

You will be prompt to create a new spreadsheet or select an existing one. Creating a new spreadsheet is highly recommended rather than using "Choose response destination".

Image 4

Click on "View Response" to open the spreadsheet.

Image 5

WinrtErrLog

As I said in the introduction, WinrtErrLog is a small C# library that helps the app to post the exception details of Google Form and save it to Google Spreadsheet. It uses asynchronous programming and HttpClient for HTTP POST request.

How to use WinrtErrLog

Now simply add WinrtErrLog in your project. But you need to extract a few things from Google Forms source code. First thing you need is the form ID. Each Google Form has a unique ID in the URL.

Click on view live form and you will be able to see the complete URL like this:

docs.google.com/forms/d/1LGucOB8X-2uhBi6ehkvzxSd4M-dcDUNGSqQ07ULl7Qs/viewform

In the above URL, form ID is “1LGucOB8X-2uhBi6ehkvzxSd4M-dcDUNGSqQ07ULl7Qs”. We also need ID for each field individually. Either Developer tool of browser or Firebug can be used to find Field ID from source code of Google Form.

This post will show you how to find the source ID of Google Form using Google Chrome. First, open live Google Form and then press F12 to open developer tools. Now you can select an element and inspect it with developer tools. To do so, select Elements tab and then click on "Magnifying Glass" button in the bottom bar. It allows to inspect the particular element within the webpage.

Image 6

Hover your cursor on the text box below "Exception Type" and then click on it. Repeat the same for others too. This will provide a list of five IDs like this:

  1. Form ID =1LGucOB8X-2uhBi6ehkvzxSd4M-dcDUNGSqQ07ULl7Qs
  2. Exception Type =36961725
  3. Exception Message = 1390140898
  4. Exception Stack Trace = 752959316
  5. Exception Source = 628132246

The Code

After extracting all required IDs one needs to provide deliberate exceptions for testing. In this post, DivideByZeroException is used for testing.

C#
private void WinrtErrLogTest()
{
    int zero = 0;
    try
    {
        var result = 9 / zero;
    }
    catch (Exception) // It will definitely throw DivideByZeroException
    {
        throw;
    }
}

WinrtErrLog has all the methods asynchronous but WinRT doesn’t allow await keyword in catch body. So a private field will be used to save the exception thrown. If private variable is not null, then exception was thrown and WinrtErrLog will handle it.

C#
private async Task WinrtErrLogTest()
{
    Exception ex = null;
    int zero = 0;
    try
    {
        var result = 9 / zero;
    }
    catch (Exception ee)
    {
        ex = ee;
    }
    if (ex != null)
    {
        // If exception thrown then WinrtErrLog will save 
        // exception details to Google spreadsheets via Google Form.
    }
}  

WinrtErrLog has only one class called ErrorLogger. And to initialize it, one needs to pass Form ID as parameter for its constructor.

Now to add the text field values, i.e., column value, there is a method called AddEntry, which takes two arguments, first is field ID which is “00000” from “entry.00000” that were extracted using developer tools and second is the Actual Value. Actual Value could be exception source, message, stack trace, etc.
C#
private async Task WinrtErrLogTest()
{
    Exception ex = null;
    int zero = 0;
    try
    {
        var result = 9 / zero;
    }
    catch (Exception ee)
    {
        ex = ee;
    }
    if (ex != null)
    {
        var objErrorLogger = new ErrorLogger("1LGucOB8X-2uhBi6ehkvzxSd4M-dcDUNGSqQ07ULl7Qs");
        
        objErrorLogger.AddEntry("36961725", ex.GetType().Name);
        objErrorLogger.AddEntry("1390140898", ex.Message);
        objErrorLogger.AddEntry("752959316", ex.StackTrace);
        objErrorLogger.AddEntry("628132246", ex.Source);
 
        var objHttpResponse = await objErrorLogger.UploadAsync();
        if (objHttpResponse.IsSuccessStatusCode)
        {
            System.Diagnostics.Debug.WriteLine("Exception logged successfully.");
        }
        else
        {
            System.Diagnostics.Debug.WriteLine
            ("Error while exception logging. HTTP status : " + objHttpResponse.StatusCode);
        }
    }
}  

Run the code to see what happens:

Image 7

NOTE: Google Spreadsheet itself adds timestamp.

In this way, with the help of Google Form and Google Spreadsheet, one can easily Log Exception Details. One can add any number of fields and analyze the crash and exception reports.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Spain Spain
-> Cloud
-> Solution Architecture
-> .NET and Sitecore Expert
-> Travel passionate

Comments and Discussions

 
QuestionLegal stuff Pin
RahulTaneja16-Aug-14 5:30
professionalRahulTaneja16-Aug-14 5:30 
AnswerRe: Legal stuff Pin
Borja Prado17-Aug-14 23:34
Borja Prado17-Aug-14 23:34 
GeneralRe: Legal stuff Pin
RahulTaneja18-Aug-14 5:24
professionalRahulTaneja18-Aug-14 5:24 
Thanks for this info.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.