Skip to content

Commit

Permalink
fixes #54
Browse files Browse the repository at this point in the history
  • Loading branch information
catalingavan committed Dec 26, 2021
1 parent f8200ff commit 4b86381
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/KissLog.AspNetCore/KissLogMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
17 changes: 14 additions & 3 deletions src/KissLog/MirrorStreamDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ public override Task<int> 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)
Expand All @@ -65,14 +69,21 @@ 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()
{
_decorated.Flush();
}

public override Task FlushAsync(CancellationToken cancellationToken)
{
return _decorated.FlushAsync(cancellationToken);
}

public override void Close()
{
_decorated.Close();
Expand Down

0 comments on commit 4b86381

Please sign in to comment.