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.
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:
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".
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".
Click on "View Response" to open the spreadsheet.
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.
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:
- Form ID =
1LGucOB8X-2uhBi6ehkvzxSd4M-dcDUNGSqQ07ULl7Qs
- Exception Type =
36961725
- Exception Message =
1390140898
- Exception Stack Trace =
752959316
- 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.
private void WinrtErrLogTest()
{
int zero = 0;
try
{
var result = 9 / zero;
}
catch (Exception)
{
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.
private async Task WinrtErrLogTest()
{
Exception ex = null;
int zero = 0;
try
{
var result = 9 / zero;
}
catch (Exception ee)
{
ex = ee;
}
if (ex != null)
{
}
}
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.
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:
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.