Ales Rosina's blog

shelastyle.net
Subscribe

Simple error reporting on Windows Phone 7



 
NEW YORK - OCTOBER 11:  A person holds a new  ...

If you’re developer, you’re familiar with user reports as “your app just crashed.” And before you can find out what the user was doing, it can last for a few hours or even days. This is even worse on mobile applications – if user will get two or three unexpected crushes, he will simply throw your app away and download a new one (don’t fool yourself, that your app is the only one with one super-cool functionality) . So getting error reports from your app is crucial for user satisfaction.

Now, let’s see how we can achieve this on Windows Phone 7. First of all, we must take into consideration how to get reports – we could use email, we could use some web service to post error reports or just store it locally and forgot about it. Sure, the last option is pretty much the same as if we didn’t have any reports and the second is also not very reliable (maybe user has no data coverage at a time of crash, maybe he’s running app in airplane mode or even a web service is down) so the only simple option is sending by email. So let’s take a look at that option.

I’m using this kind of approach in my apps with a button “report errors” in about page. This approach is widely used (eg. it’s used in Twitter and Facebook app), so I guess users are willing to use it. Let me just warn you here, that even if you have this error reporting implemented, you should still use try-catch blocks on risky parts of code.

In this example (you can download sample project here) we are going to use IsolatedStorage and Phone Task for sending email.

Firstly, in WP7 apps you can find App.xaml with codebihind in App.xaml.cs where we’ll do most of the work. In this file (which is generated by Visual Studio) you can find event called Application_UnhandledException, where you shoud add this code:

private void Application_UnhandledException(object sender, 
ApplicationUnhandledExceptionEventArgs e) { if (System.Diagnostics.Debugger.IsAttached) { // An unhandled exception has occurred; break into the debugger System.Diagnostics.Debugger.Break(); } else { try { using (IsolatedStorageFile file =
IsolatedStorageFile.GetUserStoreForApplication()) { using (StreamWriter sw = new
StreamWriter(file.OpenFile("error_log",
FileMode.Append, FileAccess.Write))) { sw.WriteLine("{0}\n{1}\n{2}\n------------------\n",
DateTime.Now.ToString(),
e.ExceptionObject.Message,
e.ExceptionObject.StackTrace); } } } catch (IsolatedStorageException ex) { } } }


Now we just need to add a button for sending reports, where we will implement sending email:

private void buttonSendError_Click(object sender, RoutedEventArgs e)
{
    using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
    {

        if (!file.FileExists("error_log"))
        {
            MessageBox.Show("Yeah, there are no errors!");
            return;
        }
        try
        {
            string errorLog = "";
            using (StreamReader reader = new 
StreamReader(file.OpenFile("error_log", FileMode.Open, FileAccess.Read))) { errorLog = reader.ReadToEnd(); Microsoft.Phone.Tasks.EmailComposeTask em = new EmailComposeTask(); em.To = "your.reporting@email.com"; em.Subject = "my super cool app has crashed! :("; em.Body = errorLog; em.Show(); } file.DeleteFile("error_log"); } catch (IsolatedStorageException ex) { } } }


And we’re done! This will open up user’s new email dialog. One downturn of send emails is, that you cannot test this in emulator, only on real device.

That’s it! If you have any comments, leave them bellow or just download sample project here.

Enhanced by Zemanta
   

author: Aleš Rosina | Comments: 4 | Tags: , , ,
23
March
2011

 
matro on 25. July 2011 at 00:42

this is a really good jumpstart, thank you from Rome, Italy. :-)

Portland injury lawyer on 20. October 2011 at 21:42

Even I get the same error on windows 7 based mobile. But after re-starting it problem solves. I still wondering what could be the reason of such errors !

Panos Papadopoulos on 11. February 2012 at 08:28

Hey, this will flood you with email. Why don't you try <a href="http://www.bugsense.com">BugSense</a>, It's pretty to use and the reports are just awesome

St. George independent living on 8. March 2012 at 01:03

It's amazing to own the possibility to browse a fantastic best high quality tutorial with beneficial data on topics that loads have an interest on. The factors which the data stated are all 1st hand on real encounters even assistance far more. Go on carrying out that which you do as we get enjoyment from looking at your obtain the task accomplished.

Leave a Comment

Name:
Email:
I respect your privacy, so your email is never published.
Web site: