From 8d4c05b27cb6b128edcd89eb9db69dfd7947d5d2 Mon Sep 17 00:00:00 2001 From: David Bennett Date: Mon, 17 Jun 2024 13:34:23 -0700 Subject: [PATCH] Diagnosability improvements - fix log flushing, log unhandled exceptions, enforce failfast of unhandled exceptions (#3211) --- src/App.xaml.cs | 23 +++++++---------------- src/MainWindow.xaml.cs | 2 ++ src/Program.cs | 14 ++++++++++++++ src/appsettings.json | 2 +- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/App.xaml.cs b/src/App.xaml.cs index 83c4640db9..f9f9c6c5d3 100644 --- a/src/App.xaml.cs +++ b/src/App.xaml.cs @@ -81,15 +81,6 @@ public App() InitializeComponent(); _dispatcherQueue = DispatcherQueue.GetForCurrentThread(); - // Set up Logging - Environment.SetEnvironmentVariable("DEVHOME_LOGS_ROOT", Path.Join(Common.Logging.LogFolderRoot, "DevHome")); - var configuration = new ConfigurationBuilder() - .AddJsonFile("appsettings.json") - .Build(); - Log.Logger = new LoggerConfiguration() - .ReadFrom.Configuration(configuration) - .CreateLogger(); - Host = Microsoft.Extensions.Hosting.Host. CreateDefaultBuilder(). UseContentRoot(AppContext.BaseDirectory). @@ -199,10 +190,15 @@ public void ShowMainWindow() private async void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) { - // TODO: Log and handle exceptions as appropriate. // https://docs.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.application.unhandledexception. - // https://github.com/microsoft/devhome/issues/613 + Log.Fatal(e.Exception, $"Unhandled exception: {e.Message}"); + + // We are about to crash, so signal the extensions to stop. await GetService().SignalStopExtensionsAsync(); + Log.CloseAndFlush(); + + // We are very likely in a bad and unrecoverable state, so ensure Dev Home crashes w/ the exception info. + Environment.FailFast(e.Message, e.Exception); } protected async override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args) @@ -225,9 +221,4 @@ private async void OnActivated(object? sender, AppActivationArguments args) // Activate the app and ensure the appropriate handlers are called. await _dispatcherQueue.EnqueueAsync(async () => await GetService().ActivateAsync(localArgsDataReference)); } - - private void Window_Closed(object sender, EventArgs e) - { - Log.CloseAndFlush(); - } } diff --git a/src/MainWindow.xaml.cs b/src/MainWindow.xaml.cs index a2b590d2a1..99f87a6802 100644 --- a/src/MainWindow.xaml.cs +++ b/src/MainWindow.xaml.cs @@ -6,6 +6,7 @@ using DevHome.Telemetry; using DevHome.TelemetryEvents; using Microsoft.UI.Xaml; +using Serilog; namespace DevHome; @@ -27,5 +28,6 @@ private void MainWindow_Closed(object sender, WindowEventArgs args) { Application.Current.GetService().SignalStopExtensionsAsync(); TelemetryFactory.Get().Log("DevHome_MainWindow_Closed_Event", LogLevel.Critical, new DevHomeClosedEvent(mainWindowCreated)); + Log.Information("Terminating via MainWindow_Closed."); } } diff --git a/src/Program.cs b/src/Program.cs index 0266a60f50..ded9d803cf 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -17,6 +17,17 @@ public static class Program [STAThread] public static void Main(string[] args) { + // Set up Logging + Environment.SetEnvironmentVariable("DEVHOME_LOGS_ROOT", Path.Join(Common.Logging.LogFolderRoot, "DevHome")); + var configuration = new ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .Build(); + Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .CreateLogger(); + + Log.Information($"Launched with args: {string.Join(' ', [.. args])}"); + // Be sure to parse these args in this instance of the exe... don't redirect this to another instance for parsing which // may be running in a different security context. ParseCommandLine(args); @@ -35,6 +46,9 @@ public static void Main(string[] args) _app = new App(); }); } + + Log.Information("Terminating Dev Home"); + Log.CloseAndFlush(); } private static async Task DecideRedirection() diff --git a/src/appsettings.json b/src/appsettings.json index 5cd6d71323..fdf37c44c6 100644 --- a/src/appsettings.json +++ b/src/appsettings.json @@ -34,7 +34,7 @@ ], "Enrich": [ "FromLogContext" ], "Properties": { - "SourceContext": "CoreWidgetExtension" + "SourceContext": "DevHome" } } }