保存例外以在下次启动时报告它们

时间:2014-01-23 10:57:27

标签: c# windows-phone-8

如果我的应用程序运行时发生错误,我将Stacktrace保存在文件中。

internal static void ReportException(Exception ex, string extra)
{
    try
    {
        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            SafeDeleteFile(store);
            TextWriter output = new StreamWriter(store.CreateFile(filename));
            output.WriteLine(extra);
            output.WriteLine(ex.Message);
            output.WriteLine(ex.StackTrace);
            output.Close();
        }
    }
    catch (Exception exf)
    {
        MessageBox.Show("Error occured while reporting an Error");
    }
}

在下次启动时,我想从这个文件中读取,但似乎文件没有被保存。

internal static void CheckForPreviousException()
{
    try
    {
        string contents = null;
        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (store.FileExists(filename))
            {
                using (TextReader reader = new StreamReader(store.OpenFile(filename, FileMode.Open, FileAccess.Read, FileShare.None)))
                {
                    contents = reader.ReadToEnd();
                }
                SafeDeleteFile(store);
            }
        }
        if (contents != null)
        {
            if (MessageBox.Show("A problem occurred the last time you ran this application. Would you like to send an email to report it?", "Problem Report", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                // Sending an Email if the user wants to
                EmailComposeTask email = new EmailComposeTask();
                email.To = mail;
                email.Subject = subject;
                email.Body = contents;
                SafeDeleteFile(IsolatedStorageFile.GetUserStoreForApplication());
                email.Show();
            }
        }
    }
    catch (Exception){}

    finally
    {
        SafeDeleteFile(IsolatedStorageFile.GetUserStoreForApplication());
    }
}

private static void SafeDeleteFile(IsolatedStorageFile store)
{
    try
    {
        store.DeleteFile(filename);
    }
    catch (Exception ex) { }
}

任何想法?

1 个答案:

答案 0 :(得分:0)

自WP8以来,只需使用后退按钮即可返回主屏幕。 从那里,您可以在应用列表中导航到您的应用 (如果您使用其中一个标准模板,则它有一个星形图标)

通过再次导航到您的应用,检查文件是否立即弹出。

如建议重新启动模拟器确实可能是您的问题的根源。