215 lines
6.3 KiB
C#
215 lines
6.3 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Microsoft.AspNetCore.Server.Kestrel.Adapter.Internal
|
|
{
|
|
internal class LoggingStream : Stream
|
|
{
|
|
private readonly Stream _inner;
|
|
private readonly ILogger _logger;
|
|
|
|
public LoggingStream(Stream inner, ILogger logger)
|
|
{
|
|
_inner = inner;
|
|
_logger = logger;
|
|
}
|
|
|
|
public override bool CanRead
|
|
{
|
|
get
|
|
{
|
|
return _inner.CanRead;
|
|
}
|
|
}
|
|
|
|
public override bool CanSeek
|
|
{
|
|
get
|
|
{
|
|
return _inner.CanSeek;
|
|
}
|
|
}
|
|
|
|
public override bool CanWrite
|
|
{
|
|
get
|
|
{
|
|
return _inner.CanWrite;
|
|
}
|
|
}
|
|
|
|
public override long Length
|
|
{
|
|
get
|
|
{
|
|
return _inner.Length;
|
|
}
|
|
}
|
|
|
|
public override long Position
|
|
{
|
|
get
|
|
{
|
|
return _inner.Position;
|
|
}
|
|
|
|
set
|
|
{
|
|
_inner.Position = value;
|
|
}
|
|
}
|
|
|
|
public override void Flush()
|
|
{
|
|
_inner.Flush();
|
|
}
|
|
|
|
public override Task FlushAsync(CancellationToken cancellationToken)
|
|
{
|
|
return _inner.FlushAsync(cancellationToken);
|
|
}
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
{
|
|
int read = _inner.Read(buffer, offset, count);
|
|
Log("Read", read, buffer, offset);
|
|
return read;
|
|
}
|
|
|
|
public async override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
|
{
|
|
int read = await _inner.ReadAsync(buffer, offset, count, cancellationToken);
|
|
Log("ReadAsync", read, buffer, offset);
|
|
return read;
|
|
}
|
|
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
{
|
|
return _inner.Seek(offset, origin);
|
|
}
|
|
|
|
public override void SetLength(long value)
|
|
{
|
|
_inner.SetLength(value);
|
|
}
|
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
{
|
|
Log("Write", count, buffer, offset);
|
|
_inner.Write(buffer, offset, count);
|
|
}
|
|
|
|
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
|
{
|
|
Log("WriteAsync", count, buffer, offset);
|
|
return _inner.WriteAsync(buffer, offset, count, cancellationToken);
|
|
}
|
|
|
|
private void Log(string method, int count, byte[] buffer, int offset)
|
|
{
|
|
var builder = new StringBuilder($"{method}[{count}] ");
|
|
|
|
// Write the hex
|
|
for (int i = offset; i < offset + count; i++)
|
|
{
|
|
builder.Append(buffer[i].ToString("X2"));
|
|
builder.Append(" ");
|
|
}
|
|
builder.AppendLine();
|
|
// Write the bytes as if they were ASCII
|
|
for (int i = offset; i < offset + count; i++)
|
|
{
|
|
builder.Append((char)buffer[i]);
|
|
}
|
|
|
|
_logger.LogDebug(builder.ToString());
|
|
}
|
|
|
|
#if NET451
|
|
// The below APM methods call the underlying Read/WriteAsync methods which will still be logged.
|
|
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
|
|
{
|
|
var task = ReadAsync(buffer, offset, count, default(CancellationToken), state);
|
|
if (callback != null)
|
|
{
|
|
task.ContinueWith(t => callback.Invoke(t));
|
|
}
|
|
return task;
|
|
}
|
|
|
|
public override int EndRead(IAsyncResult asyncResult)
|
|
{
|
|
return ((Task<int>)asyncResult).GetAwaiter().GetResult();
|
|
}
|
|
|
|
private Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken, object state)
|
|
{
|
|
var tcs = new TaskCompletionSource<int>(state);
|
|
var task = ReadAsync(buffer, offset, count, cancellationToken);
|
|
task.ContinueWith((task2, state2) =>
|
|
{
|
|
var tcs2 = (TaskCompletionSource<int>)state2;
|
|
if (task2.IsCanceled)
|
|
{
|
|
tcs2.SetCanceled();
|
|
}
|
|
else if (task2.IsFaulted)
|
|
{
|
|
tcs2.SetException(task2.Exception);
|
|
}
|
|
else
|
|
{
|
|
tcs2.SetResult(task2.Result);
|
|
}
|
|
}, tcs, cancellationToken);
|
|
return tcs.Task;
|
|
}
|
|
|
|
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
|
|
{
|
|
var task = WriteAsync(buffer, offset, count, default(CancellationToken), state);
|
|
if (callback != null)
|
|
{
|
|
task.ContinueWith(t => callback.Invoke(t));
|
|
}
|
|
return task;
|
|
}
|
|
|
|
public override void EndWrite(IAsyncResult asyncResult)
|
|
{
|
|
((Task<object>)asyncResult).GetAwaiter().GetResult();
|
|
}
|
|
|
|
private Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken, object state)
|
|
{
|
|
var tcs = new TaskCompletionSource<object>(state);
|
|
var task = WriteAsync(buffer, offset, count, cancellationToken);
|
|
task.ContinueWith((task2, state2) =>
|
|
{
|
|
var tcs2 = (TaskCompletionSource<object>)state2;
|
|
if (task2.IsCanceled)
|
|
{
|
|
tcs2.SetCanceled();
|
|
}
|
|
else if (task2.IsFaulted)
|
|
{
|
|
tcs2.SetException(task2.Exception);
|
|
}
|
|
else
|
|
{
|
|
tcs2.SetResult(null);
|
|
}
|
|
}, tcs, cancellationToken);
|
|
return tcs.Task;
|
|
}
|
|
#endif
|
|
}
|
|
}
|