Windows 8.1 introduced new classes to simplify logging. These classes are LoggingChannel , LoggingSession, and others .
Here is an example:
App.xaml.cs
LoggingSession logSession; LoggingChannel logChannel; public App() { this.InitializeComponent(); this.UnhandledException += App_UnhandledException; } void App_UnhandledException(object sender, UnhandledExceptionEventArgs e) { logChannel.LogMessage("Unhandled exception: " + e.Message); logSession.SaveToFileAsync(Windows.Storage.ApplicationData.Current.LocalFolder, "MainLog.log").AsTask().Wait(); } protected override void OnLaunched(LaunchActivatedEventArgs e) { logSession = new LoggingSession("MainLogSession"); Resources["MainLogSession"] = logSession; logChannel = new LoggingChannel("AppLogChannel"); logSession.AddLoggingChannel(logChannel); }
MainPage.xaml.cs
LoggingChannel logChannel; public MainPage() { this.InitializeComponent(); var logSession = (LoggingSession)Application.Current.Resources["MainLogSession"]; logChannel = new LoggingChannel("MainPageLogChannel"); logSession.AddLoggingChannel(logChannel); logChannel.LogMessage("MainPage ctor", LoggingLevel.Information); }
I highly recommend watching to make your Windows Store apps more reliable during the 2013 construction conference, where Harry Pearson demonstrates these new APIs in more detail (including uploading a log file to a server server using a background task that runs when the phone is connected to AC power).
source share