65.9K
CodeProject is changing. Read more.
Home

Simple Error Reporting on WP7 REDUX

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3 votes)

Feb 9, 2011

CPOL
viewsIcon

9154

Simple Error Reporting on WP7 REDUX

“Redux is a post-positive adjective meaning "brought back, restored" (from the Latin reducere - bring back) used in literature and film titles.”

After posting my last article, I received quite a lot of comments and emails about my approach, asking questions like: Would a user of your application be happy to know that it is sending emails back to a support address?

I decided to rather tell the user that “something” went wrong and then ask them if they would like to mail a report to the application developers…

A while ago, I looked at the Coding4Fun Windows Phone Toolkit, and they had the concept of a prompt (or more specific, a PopUp). Here is my prompt/PopUp:

public class ExceptionPrompt : PopUp<Exception, PopUpResult>
{
    private Button okButton;
    private CheckBox submitCheckBox;
    private Exception exception;

    public ExceptionPrompt()
    {
        DefaultStyleKey = typeof(ExceptionPrompt);
        DataContext = this;
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        if (okButton != null)
            okButton.Click -= okButton_Click;

        okButton = GetTemplateChild("okButton") as Button;
        submitCheckBox = GetTemplateChild("canSubmitCheckBox") as CheckBox;
            
        if (okButton != null)
            okButton.Click += okButton_Click;
    }

    public string To { get; set; }

    void okButton_Click(object sender, RoutedEventArgs e)
    {
        var message = new StringBuilder();
        message.Append("Exception type: ");
        message.Append(exception.GetType());
        message.Append(Environment.NewLine);
        message.Append("Message: ");
        message.Append(exception.Message);
        message.Append(Environment.NewLine);
        message.Append("Stack trace: ");
        message.Append(exception.StackTrace);
        message.ToString();

        var task = new Microsoft.Phone.Tasks.EmailComposeTask 
	{ Body = message.ToString(), Subject = "Error Report", To = To };

        if (submitCheckBox.IsChecked == true)
        {
            task.Show();          
        }

        OnCompleted(new PopUpEventArgs<Exception, PopUpResult> 
		{ PopUpResult = PopUpResult.OK });    
    }
        
    public void Show(Exception exception)
    {
        this.exception = exception; 

        base.Show();
    }
}

Now, once the exception has occurred, we just call the following code!

var exception = new ExceptionPrompt();
exception.Show(new SecurityException("Ooops, something is seriously wrong!!!"));

The user can then opt-in to send the exception to the application developers!

User now has the choice if she/he wants to send the developer the error report!

Here is the code.