-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathLoggingStream.cs
66 lines (50 loc) · 2.17 KB
/
LoggingStream.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace PowerShellEditorServices.Test.E2E
{
internal class LoggingStream : Stream
{
private static readonly string s_banner = new('=', 20);
private readonly Stream _underlyingStream;
public LoggingStream(Stream underlyingStream) => _underlyingStream = underlyingStream;
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
_underlyingStream.Dispose();
}
}
public override bool CanRead => _underlyingStream.CanRead;
public override bool CanSeek => _underlyingStream.CanSeek;
public override bool CanWrite => _underlyingStream.CanWrite;
public override long Length => _underlyingStream.Length;
public override long Position { get => _underlyingStream.Position; set => _underlyingStream.Position = value; }
public override void Flush() => _underlyingStream.Flush();
public override int Read(byte[] buffer, int offset, int count)
{
int actualCount = _underlyingStream.Read(buffer, offset, count);
LogData("READ", buffer, offset, actualCount);
return actualCount;
}
public override long Seek(long offset, SeekOrigin origin) => _underlyingStream.Seek(offset, origin);
public override void SetLength(long value) => _underlyingStream.SetLength(value);
public override void Write(byte[] buffer, int offset, int count)
{
LogData("WRITE", buffer, offset, count);
_underlyingStream.Write(buffer, offset, count);
}
private static void LogData(string header, byte[] buffer, int offset, int count)
{
Debug.WriteLine($"{header} |{s_banner.Substring(0, Math.Max(s_banner.Length - header.Length - 2, 0))}");
string data = Encoding.UTF8.GetString(buffer, offset, count);
Debug.WriteLine(data);
Debug.WriteLine(s_banner);
Debug.WriteLine("\n");
}
}
}