diff --git a/src/CalcViewModel/Common/TraceLogger.cpp b/src/CalcViewModel/Common/TraceLogger.cpp index f9268ddf..9d4bac78 100644 --- a/src/CalcViewModel/Common/TraceLogger.cpp +++ b/src/CalcViewModel/Common/TraceLogger.cpp @@ -43,6 +43,8 @@ namespace CalculatorApp constexpr auto EVENT_NAME_VARIABLE_SETTING_CHANGED = L"VariableSettingChanged"; constexpr auto EVENT_NAME_GRAPH_SETTINGS_CHANGED = L"GraphSettingsChanged"; constexpr auto EVENT_NAME_GRAPH_THEME = L"GraphTheme"; + constexpr auto EVENT_NAME_RECALL_SNAPSHOT = L"RecallSnapshot"; + constexpr auto EVENT_NAME_RECALL_RESTORE = L"RecallRestore"; constexpr auto EVENT_NAME_EXCEPTION = L"Exception"; @@ -328,4 +330,27 @@ namespace CalculatorApp TraceLoggingCommon::GetInstance()->LogLevel2Event(StringReference(EVENT_NAME_GRAPH_THEME), fields); } + + void TraceLogger::LogRecallSnapshot(ViewMode mode) + { + auto fields = ref new LoggingFields(); + fields->AddString(StringReference(CALC_MODE), NavCategoryStates::GetFriendlyName(mode)); + TraceLoggingCommon::GetInstance()->LogLevel2Event(StringReference(EVENT_NAME_RECALL_SNAPSHOT), fields); + } + + void TraceLogger::LogRecallRestore(ViewMode mode) + { + auto fields = ref new LoggingFields(); + fields->AddString(StringReference(CALC_MODE), NavCategoryStates::GetFriendlyName(mode)); + TraceLoggingCommon::GetInstance()->LogLevel2Event(StringReference(EVENT_NAME_RECALL_RESTORE), fields); + } + + void TraceLogger::LogRecallError(CalculatorApp::ViewModel::Common::ViewMode mode, Platform::String^ message) + { + auto fields = ref new LoggingFields(); + fields->AddString(StringReference(CALC_MODE), NavCategoryStates::GetFriendlyName(mode)); + fields->AddString(StringReference(L"FunctionName"), L"Recall"); + fields->AddString(StringReference(L"Message"), message); + TraceLoggingCommon::GetInstance()->LogLevel2Event(StringReference(EVENT_NAME_EXCEPTION), fields); + } } diff --git a/src/CalcViewModel/Common/TraceLogger.h b/src/CalcViewModel/Common/TraceLogger.h index e82d3645..25e57eea 100644 --- a/src/CalcViewModel/Common/TraceLogger.h +++ b/src/CalcViewModel/Common/TraceLogger.h @@ -85,6 +85,9 @@ namespace CalculatorApp::ViewModel::Common void LogGraphTheme(Platform::String ^ graphTheme); void LogInputPasted(CalculatorApp::ViewModel::Common::ViewMode mode); void LogPlatformExceptionInfo(CalculatorApp::ViewModel::Common::ViewMode mode, Platform::String ^ functionName, Platform::String ^ message, int hresult); + void LogRecallSnapshot(CalculatorApp::ViewModel::Common::ViewMode mode); + void LogRecallRestore(CalculatorApp::ViewModel::Common::ViewMode mode); + void LogRecallError(CalculatorApp::ViewModel::Common::ViewMode mode, Platform::String ^ message); internal: void LogPlatformException(CalculatorApp::ViewModel::Common::ViewMode mode, Platform::String ^ functionName, Platform::Exception ^ e); diff --git a/src/Calculator/Views/MainPage.xaml.cs b/src/Calculator/Views/MainPage.xaml.cs index 829af697..ab60ba7c 100644 --- a/src/Calculator/Views/MainPage.xaml.cs +++ b/src/Calculator/Views/MainPage.xaml.cs @@ -78,6 +78,7 @@ namespace CalculatorApp await activity.SaveAsync(); args.Request.SetUserActivity(activity); deferral.Complete(); + TraceLogger.GetInstance().LogRecallSnapshot(Model.Mode); }; } @@ -169,17 +170,14 @@ namespace CalculatorApp var channel = UserActivityChannel.GetDefault(); var activity = await channel.GetOrCreateUserActivityAsync(snapshotArgs.ActivityId); - // Work around for bug https://microsoft.visualstudio.com/DefaultCollection/OS/_workitems/edit/48931227 - // where ContentInfo can't be directly accessed. - if (snapshotArgs.VerifyIncomingActivity(activity) && - JsonObject.TryParse(activity.ToJson(), out var activityJson) && - activityJson.ContainsKey("contentInfo") && - Model.TryRestoreFromSnapshot(activityJson.GetNamedObject("contentInfo"))) + if (TryRestoreFromActivity(snapshotArgs, activity, out var errorMessage)) { + TraceLogger.GetInstance().LogRecallRestore(Model.Mode); SelectNavigationItemByModel(); } else { + TraceLogger.GetInstance().LogRecallError(Model.Mode, errorMessage); await ShowSnapshotLaunchErrorAsync(); } }); @@ -191,6 +189,38 @@ namespace CalculatorApp } } + private bool TryRestoreFromActivity(SnapshotLaunchArguments snapshotArgs, UserActivity activity, out string errorMessage) + { + if (!snapshotArgs.VerifyIncomingActivity(activity)) + { + errorMessage = "IncomingActivityFailed"; + return false; + } + + // Work around for bug https://microsoft.visualstudio.com/DefaultCollection/OS/_workitems/edit/48931227 + // where ContentInfo can't be directly accessed. + if (!JsonObject.TryParse(activity.ToJson(), out var activityJson)) + { + errorMessage = "ParseJsonError"; + return false; + } + + if (!activityJson.ContainsKey("contentInfo")) + { + errorMessage = "ContentInfoNotExist"; + return false; + } + + if (!Model.TryRestoreFromSnapshot(activityJson.GetNamedObject("contentInfo"))) + { + errorMessage = "RestoreFromSnapshotFailed"; + return false; + } + + errorMessage = string.Empty; + return true; + } + private void InitializeNavViewCategoriesSource() { NavViewCategoriesSource = ExpandNavViewCategoryGroups(Model.Categories);