Skip to content

Commit

Permalink
Better handle UnobservedTaskExceptions without the Event Aggregator. … (
Browse files Browse the repository at this point in the history
#539)

* Better handle UnobservedTaskExceptions without the Event Aggregator.  Logs will have them.

* formatting
  • Loading branch information
adamhathcock authored Jan 30, 2025
1 parent 48e66f9 commit f379996
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions DUI3/Speckle.Connectors.DUI/ContainerRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,27 @@ public static IServiceCollection AddEventsAsTransient(this IServiceCollection se
public static IServiceProvider UseDUI(this IServiceProvider serviceProvider, bool initializeDocumentStore = true)
{
//observe the unobserved!
TaskScheduler.UnobservedTaskException += async (_, args) =>
TaskScheduler.UnobservedTaskException += (_, args) =>
{
await serviceProvider
.GetRequiredService<IEventAggregator>()
.GetEvent<ExceptionEvent>()
.PublishAsync(args.Exception);
serviceProvider.GetRequiredService<ILogger>().LogError(args.Exception, "Unobserved task exception");
args.SetObserved();
try
{
serviceProvider
.GetRequiredService<ILoggerFactory>()
.CreateLogger("UnobservedTaskException")
.LogError(args.Exception, "Unobserved task exception");
}
#pragma warning disable CA1031
catch (Exception e)
#pragma warning restore CA1031
{
Console.WriteLine("Error logging unobserved task exception");
Console.WriteLine(args.Exception);
Console.WriteLine(e);
}
finally
{
args.SetObserved();
}
};

if (initializeDocumentStore)
Expand Down

0 comments on commit f379996

Please sign in to comment.