Skip to content

Commit cc99a0b

Browse files
committed
Make reporting of unhandled exceptions more useful
1 parent 14a021b commit cc99a0b

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

Rubberduck.Main/Extension.cs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private void Startup()
232232
try
233233
{
234234
var currentDomain = AppDomain.CurrentDomain;
235-
currentDomain.UnhandledException += HandlAppDomainException;
235+
currentDomain.UnhandledException += HandleAppDomainException;
236236
currentDomain.AssemblyResolve += LoadFromSameFolder;
237237

238238
_container = new WindsorContainer().Install(new RubberduckIoCInstaller(_vbe, _addin, _initialSettings, _vbeNativeApi, _beepInterceptor));
@@ -244,27 +244,38 @@ private void Startup()
244244
}
245245
catch (Exception e)
246246
{
247-
_logger.Log(LogLevel.Fatal, e, "Startup sequence threw an unexpected exception.");
247+
_logger.Fatal(e, "Startup sequence threw an unexpected exception.");
248248
throw new Exception("Rubberduck's startup sequence threw an unexpected exception. Please check the Rubberduck logs for more information and report an issue if necessary", e);
249249
}
250250
}
251251

252-
private void HandlAppDomainException(object sender, UnhandledExceptionEventArgs e)
252+
private void HandleAppDomainException(object sender, UnhandledExceptionEventArgs e)
253253
{
254-
_logger.Log(LogLevel.Fatal, e);
254+
var message = e.IsTerminating
255+
? "An unhandled exception occurred. The runtime is shutting down."
256+
: "An unhandled exception occurred. The runtime continues running.";
257+
if (e.ExceptionObject is Exception exception)
258+
{
259+
_logger.Fatal(exception, message);
260+
261+
}
262+
else
263+
{
264+
_logger.Fatal(message);
265+
}
255266
}
256267

257268
private void ShutdownAddIn()
258269
{
259270
var currentDomain = AppDomain.CurrentDomain;
260271
try
261272
{
262-
_logger.Log(LogLevel.Info, "Rubberduck is shutting down.");
263-
_logger.Log(LogLevel.Trace, "Unhooking VBENativeServices events...");
273+
_logger.Info("Rubberduck is shutting down.");
274+
_logger.Trace("Unhooking VBENativeServices events...");
264275
VbeNativeServices.UnhookEvents();
265276
VbeProvider.Terminate();
266277

267-
_logger.Log(LogLevel.Trace, "Releasing dockable hosts...");
278+
_logger.Trace("Releasing dockable hosts...");
268279

269280
using (var windows = _vbe.Windows)
270281
{
@@ -273,48 +284,48 @@ private void ShutdownAddIn()
273284

274285
if (_app != null)
275286
{
276-
_logger.Log(LogLevel.Trace, "Initiating App.Shutdown...");
287+
_logger.Trace("Initiating App.Shutdown...");
277288
_app.Shutdown();
278289
_app = null;
279290
}
280291

281292
if (_container != null)
282293
{
283-
_logger.Log(LogLevel.Trace, "Disposing IoC container...");
294+
_logger.Trace("Disposing IoC container...");
284295
_container.Dispose();
285296
_container = null;
286297
}
287298
}
288299
catch (Exception e)
289300
{
290301
_logger.Error(e);
291-
_logger.Log(LogLevel.Warn, "Exception is swallowed.");
302+
_logger.Warn("Exception is swallowed.");
292303
//throw; // <<~ uncomment to crash the process
293304
}
294305
finally
295306
{
296307
try
297308
{
298-
_logger.Log(LogLevel.Trace, "Disposing COM safe...");
309+
_logger.Trace("Disposing COM safe...");
299310
ComSafeManager.DisposeAndResetComSafe();
300311
_addin = null;
301312
_vbe = null;
302313

303314
_isInitialized = false;
304-
_logger.Log(LogLevel.Info, "No exceptions were thrown.");
315+
_logger.Info("No exceptions were thrown.");
305316
}
306317
catch (Exception e)
307318
{
308319
_logger.Error(e);
309-
_logger.Log(LogLevel.Warn, "Exception disposing the ComSafe has been swallowed.");
320+
_logger.Warn("Exception disposing the ComSafe has been swallowed.");
310321
//throw; // <<~ uncomment to crash the process
311322
}
312323
finally
313324
{
314-
_logger.Log(LogLevel.Trace, "Unregistering AppDomain handlers....");
325+
_logger.Trace("Unregistering AppDomain handlers....");
315326
currentDomain.AssemblyResolve -= LoadFromSameFolder;
316-
currentDomain.UnhandledException -= HandlAppDomainException;
317-
_logger.Log(LogLevel.Trace, "Done. Main Shutdown completed. Toolwindows follow. Quack!");
327+
currentDomain.UnhandledException -= HandleAppDomainException;
328+
_logger.Trace( "Done. Main Shutdown completed. Toolwindows follow. Quack!");
318329
_isInitialized = false;
319330
}
320331
}

0 commit comments

Comments
 (0)