Skip to content

Commit

Permalink
Diagnosability improvements - fix log flushing, log unhandled excepti…
Browse files Browse the repository at this point in the history
…ons, enforce failfast of unhandled exceptions (#3211)
  • Loading branch information
dkbennett authored Jun 17, 2024
1 parent c905a49 commit 8d4c05b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
23 changes: 7 additions & 16 deletions src/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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<IExtensionService>().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)
Expand All @@ -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<IActivationService>().ActivateAsync(localArgsDataReference));
}

private void Window_Closed(object sender, EventArgs e)
{
Log.CloseAndFlush();
}
}
2 changes: 2 additions & 0 deletions src/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using DevHome.Telemetry;
using DevHome.TelemetryEvents;
using Microsoft.UI.Xaml;
using Serilog;

namespace DevHome;

Expand All @@ -27,5 +28,6 @@ private void MainWindow_Closed(object sender, WindowEventArgs args)
{
Application.Current.GetService<IExtensionService>().SignalStopExtensionsAsync();
TelemetryFactory.Get<ITelemetry>().Log("DevHome_MainWindow_Closed_Event", LogLevel.Critical, new DevHomeClosedEvent(mainWindowCreated));
Log.Information("Terminating via MainWindow_Closed.");
}
}
14 changes: 14 additions & 0 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -35,6 +46,9 @@ public static void Main(string[] args)
_app = new App();
});
}

Log.Information("Terminating Dev Home");
Log.CloseAndFlush();
}

private static async Task<bool> DecideRedirection()
Expand Down
2 changes: 1 addition & 1 deletion src/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
],
"Enrich": [ "FromLogContext" ],
"Properties": {
"SourceContext": "CoreWidgetExtension"
"SourceContext": "DevHome"
}
}
}

0 comments on commit 8d4c05b

Please sign in to comment.