From 4b86381b2be4d57400a61114a88f23a9b6a6843e Mon Sep 17 00:00:00 2001 From: catalingavan Date: Sun, 26 Dec 2021 11:39:56 +0100 Subject: [PATCH] fixes #54 --- src/KissLog.AspNetCore/KissLogMiddleware.cs | 5 ++++- src/KissLog/MirrorStreamDecorator.cs | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/KissLog.AspNetCore/KissLogMiddleware.cs b/src/KissLog.AspNetCore/KissLogMiddleware.cs index a6009cf..5ca5c8e 100644 --- a/src/KissLog.AspNetCore/KissLogMiddleware.cs +++ b/src/KissLog.AspNetCore/KissLogMiddleware.cs @@ -44,7 +44,10 @@ public async Task Invoke(HttpContext context) ExceptionDispatchInfo ex = null; - context.Response.Body = new MirrorStreamDecorator(context.Response.Body); + if(context.Response.Body != null && context.Response.Body is MirrorStreamDecorator == false) + { + context.Response.Body = new MirrorStreamDecorator(context.Response.Body); + } try { diff --git a/src/KissLog/MirrorStreamDecorator.cs b/src/KissLog/MirrorStreamDecorator.cs index 670a2f8..9f0c716 100644 --- a/src/KissLog/MirrorStreamDecorator.cs +++ b/src/KissLog/MirrorStreamDecorator.cs @@ -48,13 +48,17 @@ public override Task ReadAsync(byte[] buffer, int offset, int count, Cancel public override void Write(byte[] buffer, int offset, int count) { _decorated.Write(buffer, offset, count); - _mirrorStream.Write(buffer, offset, count); + + if(_mirrorStream?.CanWrite == true) + _mirrorStream.Write(buffer, offset, count); } public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { await _decorated.WriteAsync(buffer, offset, count, cancellationToken); - _mirrorStream.Write(buffer, offset, count); + + if (_mirrorStream?.CanWrite == true) + _mirrorStream.Write(buffer, offset, count); } public override long Seek(long offset, SeekOrigin origin) @@ -65,7 +69,9 @@ public override long Seek(long offset, SeekOrigin origin) public override void SetLength(long value) { _decorated.SetLength(value); - _mirrorStream.SetLength(value); + + if (_mirrorStream?.CanWrite == true) + _mirrorStream.SetLength(value); } public override void Flush() @@ -73,6 +79,11 @@ public override void Flush() _decorated.Flush(); } + public override Task FlushAsync(CancellationToken cancellationToken) + { + return _decorated.FlushAsync(cancellationToken); + } + public override void Close() { _decorated.Close();