If you haven’t read Rudi Grobler’s Simple Error Reporting on WP7 and Simple Error Reporting on WP7 Redux, please go read them now. Basically it’s about reporting errors at runtime from a Windows Phone 7 application. I always incorporate this kind of functionality in my applications (not only on Windows Phone) and it has proven to be very useful in these years.
While Rudi’s implementation uses the EmailComposeTask to send an error report, I do it in a slightly different and by submitting the report as http POST to a web page that then sends the email. This way the process is entirely silent and nothing is shown to the user (except the prompt). My reasoning is that the less steps she has to perform, the more likely she is to send the report.
In order to use this method you’ll need some server, but nothing fancy is required. I suspect you already have something adequate in place (for example the one hosting your blog).
Client Side
This is the code I put on the phone:
public void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { if (MessageBoxResult.OK != MessageBox.Show("We encountered a problem and need to close. Would you like to tell us?", "Oh no!", MessageBoxButton.OKCancel)) { return; } try { var subj = e.ExceptionObject.Message; var body = new StringBuilder("Stacktrace:"); body.AppendLine(e.ExceptionObject.StackTrace); var wc = new WebClient(); wc.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0"; wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; wc.UploadStringCompleted += (s, er) => MessageBox.Show("Thanks for your report!"); wc.UploadStringAsync( new Uri("http://your-host.com/bug-report.php"), "POST", string.Format("subject={0}&body={1}", HttpUtility.UrlEncode(subj), HttpUtility.UrlEncode(body.ToString()))); } catch (WebException) { MessageBox.Show("The report could not be sent (no network available?)."); } }
Don’t forget to replace the messages with something better. You can add whatever you like to the report, just keep in mind that if you query DeviceExtendedProperties to get the peak memory usage, your app will need the Identity Capability (which looks a bit scary in the marketplace in my opinion).
Server Side
On the server side you can do pretty much anything: you could insert the report in a database or fill in a ticket in your bug tracker. I keep it simple and send an email (please set up a dedicated address if you do this). My server is rather fast but only supports PHP, so that’s what I use:
<?php if (isset($_POST["subject"]) &amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp; isset($_POST["body"])) { mail("youremail@your-host.com", "wp7 app bug report: ".$_POST["subject"], $_POST["body"]); echo "ok."; } else { echo "missing parameters."; } ?>
Please notice that this is not the most secure practice in the world as someone could sniff the traffic generated by your app and exploit this page to flood your mailbox. A simple solution is to limit this page to send a maximum of say 10 emails/hour. Anyways if you are getting a much larger number of legit hits, chances are that the exception is always the same one (or your app is really buggy and you deserve to be flooded :-P).
Is this method better than using the EmailComposeTask? I don’t know, but it’s just another option at your disposal.