This code works fine in my WP8 app:
void App_UnhandledException(object sender, UnhandledExceptionEventArgs args)
{
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");
}
WAMS_EXCEPTIONLOG wamsel = new WAMS_EXCEPTIONLOG
{
appNameAndVersion =
string.Format("{0} {1}", appName,
appVersion),
ExceptionMsg =
args.ExceptionObject.Message,
InnerException =
args.ExceptionObject
.InnerException.ToString(),
ExceptionToStr =
args.ExceptionObject.ToString(),
dateTimeOffsetStamp =
DateTimeOffset.UtcNow
};
await MobileService.GetTable<TASLS_WAMS_EXCEPTIONLOG>().InsertAsync(wamsel);
}
...but in my complementary Windows store app, several classes and class members are unrecognized, to wit:
XmlResolver
XmlXapResolver
args.ExceptionObject
(not to mention the fact that await is not allowed, and adding "async" to the event handler causes the assignment of the event handler to "go red")...
So, to get back to the main point: How can I achieve the same functionality I'm getting with my WP8 app with my Windows Store app?
Let me first address your issues:
args.Exception
.async void
in the method signature but you must keep in mind that the method will be called in "fire and forget" mode, i.e. the app won't wait for the asynchronous method to complete.This shouldn't be a problem if you set args.Handled = true
and thus prevent the app from closing.Your fixed event handler should look like this:
private async void App_UnhandledException(object sender, UnhandledExceptionEventArgs args)
{
string appName = Package.Current.Id.Name;
var version = Package.Current.Id.Version;
string appVersion = String.Format("{0}.{1}.{2}.{3}",
version.Major, version.Minor, version.Build, version.Revision);
WAMS_EXCEPTIONLOG wamsel = new WAMS_EXCEPTIONLOG
{
appNameAndVersion = string.Format("{0} {1}", appName, appVersion),
ExceptionMsg = args.Exception.Message,
InnerException = args.Exception.InnerException.ToString(),
ExceptionToStr = args.Exception.ToString(),
dateTimeOffsetStamp = DateTimeOffset.UtcNow
};
args.Handled = true;
await MobileService.GetTable<TASLS_WAMS_EXCEPTIONLOG>().InsertAsync(wamsel);
}
You should also check if args.Exception.InnerException
is null, before calling ToString()
on it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With