WP7 Application Crash Reporter

Update Feb. 26, 2011: Crash logger code has been modified to provide more details, including the OS version, current culture, current XAML page, and whether the app is obscured or locked.

Recently I found Andy Pennell’s LittleWatson Class, which is designed to log and report any unexpected crashes in your WP7 app. This can provide extremely useful data about problems your users have experienced and, if you tend to run your app without debugging enabled, crashes that occur during development.

Andy’s code is designed to send error reports via e-mail, but I’d prefer something a bit less intrusive. For Remote, I decided to write a class similar to LittleWatson that would submit the error reports to a script running on my web server.

CrashReporter

The first step is to add the CrashReporter class to your application.

Initialize is the only public member of this class and should be called from the App constructor in App.xaml.cs (see below).

The crash log is only cleared once CrashReporter has successfully sent the log to your web server, so if your server is down or unreachable the app will try to submit the log again the next time it is launched.

Utility Class

This class makes use of the Utility class I covered in my previous blog post. The relevant parts of this class appear below:

App.xaml.cs

Once the CrashReporter and Utility classes have been added to your application, you can call the CrashReporter’s Initialize method from the constructor in App.xaml.cs. CrashReporter.Initialize must be called after RootFrame has been initialized, so it should be placed near the end of the constructor as follows:

Listening for Crash Reports

The following PHP script can save each crash report to a file, e-mail you for each crash report, or both.

If you want to log each crash report to a file, make sure the PHP script has permission to create files. (This may require the use of chmod, depending on your server’s configuration.) The script will automatically attempt to create a directory called ./reports/productname/ beneath the current directory. If you use this method, make sure you either password protect the reports directory or configure your web server to deny access to that directory (e.g., put deny from all in an .htaccess file inside the reports directory).

If you want to send each crash report by e-mail, set $send_email to true on line 8 and enter your e-mail address on line 9. This script assumes PHP’s mail() has been configured properly on your server.

Testing

Once everything has been configured, the next step is to test it out. The easiest way is to intentionally trigger an exception in your app. After the app closes, relaunch it and the log should be sent to your server.


That’s it! If you find this code useful or make any improvements to it, please let me know! Also, follow me on Twitter and check out my app, Remote for Windows Phone 7.

Leave a Reply

Your email address will not be published. Required fields are marked *