diff --git a/src/Http/LogStream.cs b/src/Http/LogStream.cs index 1265f8b..1a8d07e 100644 --- a/src/Http/LogStream.cs +++ b/src/Http/LogStream.cs @@ -560,8 +560,18 @@ protected virtual void WriteLineInternal ( string line ) { string lineText = NormalizeEntries ? line.Normalize ().Trim ().ReplaceLineEndings () : line; - if (!_channel.Writer.TryWrite ( lineText )) { - throw new InvalidOperationException ( SR.LogStream_FailedWrite ); + try { + _channel.Writer.WriteAsync ( lineText, _cancellationTokenSource.Token ) + .AsTask () + .ConfigureAwait ( false ) + .GetAwaiter () + .GetResult (); + } + catch (ChannelClosedException) { + // Channel was closed, which is expected during shutdown. Ignore. + } + catch (OperationCanceledException) when (_cancellationTokenSource.IsCancellationRequested) { + // Cancellation requested during shutdown. Ignore. } } @@ -738,4 +748,4 @@ private sealed class FlushSignal { public TaskCompletionSource Completion { get; } = new TaskCompletionSource ( TaskCreationOptions.RunContinuationsAsynchronously ); } } -} \ No newline at end of file +}