Skip to content

Add IFlushableLogSink interface and LogManager.FlushAsync#13

Merged
DJGosnell merged 1 commit intomasterfrom
issue-9/iflushablelogsink-flush-async
Mar 3, 2026
Merged

Add IFlushableLogSink interface and LogManager.FlushAsync#13
DJGosnell merged 1 commit intomasterfrom
issue-9/iflushablelogsink-flush-async

Conversation

@DJGosnell
Copy link
Member

Summary

Reason for Change

There was no way to force-flush buffered sinks without shutting down. After a critical error, an application may need to ensure all buffered log entries are written to disk/network before attempting recovery — without tearing down the logging pipeline.

Impact

  • BufferedLogSink (and its subclasses FileSink, StreamSink) now implement IFlushableLogSink
  • Non-buffered sinks (ConsoleSink, DebugSink, RecordingSink) are unaffected — they don't implement the interface and are simply skipped during flush
  • LogManager.FlushAsync() iterates all sinks, calling FlushAsync only on IFlushableLogSink implementations

Migration Steps

None required — this is purely additive. Existing code continues to work unchanged.

Performance Considerations

  • Flush uses a TCS sentinel enqueued into the existing channel — zero additional locks or synchronization primitives
  • CreateFlushSentinel uses Array.Empty<byte>() to avoid allocation for the unused buffer field
  • TaskCreationOptions.RunContinuationsAsynchronously prevents inline continuation on the drain loop thread
  • No performance impact on normal (non-flush) write paths — the IsFlushSentinel check is a single null reference check

Security Considerations

None.

Breaking Changes

  • Consumer-facing: None
  • Internal: BufferedEntry record struct gains an optional FlushCompletion parameter (default null) — source-compatible for existing derived sinks

Introduce a flush primitive for buffered sinks that drains queued entries
without tearing down the logging pipeline.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@DJGosnell DJGosnell merged commit 7d99d74 into master Mar 3, 2026
1 check passed
@DJGosnell DJGosnell deleted the issue-9/iflushablelogsink-flush-async branch March 3, 2026 18:29
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.

IFlushableLogSink interface and LogManager.FlushAsync

1 participant