Quantcast
Channel: Blog – James Croft
Viewing all articles
Browse latest Browse all 63

UWP exception handling and logging with WinUX

$
0
0

When it comes to exception handling, there is always the odd occasion where you forgot to put a null check or try-catch which results in an exception being thrown in your application resulting in it falling flat on it’s face.

To resolve this in my own projects, I took advantage of the global exception handling events Application.Current.UnhandledException and TaskScheduler.UnobservedTaskException. Combined, these events will allow me to handle any uncaught exceptions without having my app suddenly stop working.

Within the WinUX Core library, I recently added an AppLogHandler as a request from a fellow who tweeted me after I recently updated the WinUX project on NuGet.

AppLoghandler takes advantage of a feature that was pre-existing in the WinUX project since it’s inception last year which allowed you to set up an event log in your application. The event log can be used for a variety of different things, not just for logging errors. This is especially great when you’re wanting to write out some debug information to a file to review.

What does AppLogHandler do?

When started, the handler will create a file in the local folder of your application with a file name prefixed with log followed by the date the log was created. When a user uses the application, it will look to see if a file for the current day already exists and will append to it.

Starting the handler will also setup for listening to events that you log using the WinUX Logger.Log as well as handling the events that I mentioned earlier for exceptions.

When an exception is thrown in your application, if you don’t handle it, it will automatically be handled and logged out to the log file with the stack trace if one exists. This will hopefully make it easier to diagnose where issues are popping up in your application.

How to use the AppLogHandler

To make this as simple as possible using a single line of code, all you need to do in your application is add a reference to the WinUX NuGet package, or just WinUX.UWP.Core, and add the following line into your OnLaunched method in the App.xaml.cs file.

If you have any question on how this works or just a general question about WinUX, feel free to get in touch.

You can keep up-to-date with the progress of the WinUX project over on GitHub.


Viewing all articles
Browse latest Browse all 63

Trending Articles