Skip to content

Move CaptureUnhandledExceptions into config builder#14

Merged
DJGosnell merged 1 commit intomasterfrom
feature/10-capture-exceptions-in-builder
Mar 3, 2026
Merged

Move CaptureUnhandledExceptions into config builder#14
DJGosnell merged 1 commit intomasterfrom
feature/10-capture-exceptions-in-builder

Conversation

@DJGosnell
Copy link
Member

Summary

Reason for Change

CaptureUnhandledExceptions was a standalone static method on LogManager with a lifetime decoupled from the logging configuration. Developers had to manually call StopCapturingUnhandledExceptions(), and forgetting to do so leaked event subscriptions. Moving it into the config builder ties its lifecycle to Reconfigure() and Shutdown().

Impact

  • LogConfigBuilder gains CaptureUnhandledExceptions(bool observeTaskExceptions = false) method
  • LogConfig stores capture flags; handlers are automatically subscribed/unsubscribed during config swaps
  • Event handlers now read from _config?.ErrorHandler (the InternalErrorHandler) instead of a separate static delegate
  • SetMinimumLevel/SetCategoryOverrides internal methods carry exception capture flags to new configs

Migration Steps

Replace:

LogManager.CaptureUnhandledExceptions(ex => Log.Error(ex), observeTaskExceptions: true);
// ... later ...
LogManager.StopCapturingUnhandledExceptions();

With:

LogManager.Initialize(cfg =>
{
    cfg.InternalErrorHandler = ex => Console.Error.WriteLine(ex);
    cfg.CaptureUnhandledExceptions(observeTaskExceptions: true);
    // ...
});
// Automatically unsubscribed on Reconfigure() or Shutdown()

Performance Considerations

No performance impact — same Interlocked guard pattern, same event handler logic.

Security Considerations

None.

Breaking Changes

  • Consumer-facing: LogManager.CaptureUnhandledExceptions() and LogManager.StopCapturingUnhandledExceptions() public methods removed. Exception handler parameter removed — routes to InternalErrorHandler instead.
  • Internal: _exceptionHandler static field removed from LogManager.

Tie exception capture lifecycle to LogConfig so handlers are
automatically unsubscribed on Reconfigure() and Shutdown(),
eliminating the need for manual StopCapturing calls.

Breaking change: remove LogManager.CaptureUnhandledExceptions()
and LogManager.StopCapturingUnhandledExceptions() public methods.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@DJGosnell DJGosnell force-pushed the feature/10-capture-exceptions-in-builder branch from f555364 to fe29a61 Compare March 3, 2026 18:30
@DJGosnell DJGosnell merged commit c6b878e into master Mar 3, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move CaptureUnhandledExceptions into config builder

1 participant