IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
StreamDiagnosticChannel.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.IO.Pipelines;
4using System.Text;
5
7{
8
13 {
14
15 readonly Stream _stream;
16 readonly PipeWriter _pipe;
17 readonly AsyncDiagnosticChannelWriter _writer;
18 readonly Encoding _encoding;
19
26 public StreamDiagnosticChannel(Stream stream, Encoding encoding, bool leaveOpen = true)
27 {
28 _stream = stream ?? throw new ArgumentNullException(nameof(stream));
29 _pipe = PipeWriter.Create(_stream, new StreamPipeWriterOptions(leaveOpen: leaveOpen));
30 _writer = new AsyncDiagnosticChannelWriter(_pipe);
31 _encoding = encoding ?? throw new ArgumentNullException(nameof(encoding));
32 }
33
36
38 public Encoding Encoding => _encoding;
39
43 public void Dispose()
44 {
45 // dispose the writer, which causes the background writer to terminate and releases any buffers, but does not close the pipe
46 _writer.Dispose();
47
48 // flush and close the pipe, which should close the stream if requested
49 _pipe.FlushAsync().AsTask().GetAwaiter().GetResult();
50 _pipe.Complete();
51 }
52
53 }
54
55}
Wraps a IBufferWriter<T> and queues data writen to it. Requires disposal to ensure flushing.
Implementation of IDiagnosticChannel that writes encoded text to a PipeWriter.
IDiagnosticChannelWriter Writer
Gets the writer that allows you to enqueue data to the channel.
StreamDiagnosticChannel(Stream stream, Encoding encoding, bool leaveOpen=true)
Initializes a new instance.
Handles consuming and outputting diagnostic events.