我想在 UnhandledException 事件中填充一个 WAMS 表,并且我有以下代码:
private async void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs args)
{
if (Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
Debugger.Break();
}
PLATYPIRUS_WAMS_EXCEPTIONLOG pruwamsel = new PLATYPIRUS_WAMS_EXCEPTIONLOG();
pruwamsel.appNameAndVersion = "Platypi R Us for WP8 v. 3.14";
pruwamsel.ExceptionMsg = args.ExceptionObject.Message;
pruwamsel.InnerException = args.ExceptionObject.InnerException.ToString();
pruwamsel.ExceptionToStr = args.ToString();
pruwamsel.dateTimeOffsetStamp = DateTimeOffset.UtcNow;
await App.MobileService.GetTable<PLATYPIRUS_WAMS_EXCEPTIONLOG>().InsertAsync(pruwamsel);
}
...但我真的不想硬编码应用程序名称和版本。如何以编程方式提取这些?
更新
结合这两个想法,我最终得到:
private async void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs args)
{
if (Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
Debugger.Break();
}
string appName;
string appVersion;
var xmlReaderSettings = new XmlReaderSettings
{
XmlResolver = new XmlXapResolver()
};
using (var xmlReader = XmlReader.Create("WMAppManifest.xml", xmlReaderSettings))
{
xmlReader.ReadToDescendant("App");
appName = xmlReader.GetAttribute("Title");
appVersion = xmlReader.GetAttribute("Version");
}
PLATYPIRUS_WAMS_EXCEPTIONLOG pruwamsel = new PLATYPIRUS_WAMS_EXCEPTIONLOG();
pruwamsel.appNameAndVersion = string.Format("{0} {1}", appName, appVersion);
pruwamsel.ExceptionMsg = args.ExceptionObject.Message;
pruwamsel.InnerException = args.ExceptionObject.InnerException.ToString();
pruwamsel.ExceptionToStr = args.ExceptionObject.ToString();
pruwamsel.dateTimeOffsetStamp = DateTimeOffset.UtcNow;
await App.MobileService.GetTable<PLATYPIRUS_WAMS_EXCEPTIONLOG>().InsertAsync(pruwamsel);
}