如何设置StreamWriter';的源在.NET中为多个
本文关键字:NET 何设置 设置 StreamWriter | 更新日期: 2023-09-27 18:24:23
我正在.NET中开发一些网络程序。我想在我的输出窗口上看到整个文本流作为日志。因此,我尝试在StreamWriter上搜索设置多个源的解决方案。但我找不到。我想要像下面这样的样式而不是扩展方法:
var sw = new StreamWriter(socketStream, traceStream)
或
var sw = new StreamWriter(socketStream);
sw.setSecondStream(traceStream);
制作一个流分离器应该没那么难。对于没有Seek函数的仅写流来说,这非常简单。
构造函数调用将看起来像
var sw = new StreamWriter(new WriteStreamSplitter(socketStream, traceStream));
和班级(未经测试,只是一个模型)
class WriteStreamSplitter : Stream
{
public WriteStreamSplitter(Stream a, Stream b)
{
_streamA = a;
_streamB = b;
}
public override void Write(byte[] buffer, int offset, int count)
{
_streamA.Write(buffer, offset, count);
_streamB.Write(buffer, offset, count);
}
public override bool CanWrite { get { return true; } }
public override bool CanRead { get { return false; } }
// trivial overloads of all other abstract members,
// they are allowed to throw NotImplemented
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
// Maybe it's best to do nothing here but a StreamWriter
// assumes 'ownership' of the wrapped stream,
// so if we want to continue that pattern:
// the guidelines say these shouldn't throw
_streamA.Dispose();
_streamB.Dispose();
}
}
}
我认为您应该采取不同的方法。创建一个类并聚合StreamWriter。然后使用MyStreamWriter写入流,而不是直接使用StreamWriter:
class MyStreamWriter
{
private StreamWriter sw;
// Constructor
public MyStreamWriter(Stream socketStream)
{
sw = new StreamWriter(socketStream);
}
// Example function
public void WriteLine(string myText)
{
Trace.WriteLine(myText);
sw.WriteLine(myText);
}
}
这允许您附加附加功能。
与其使用代码创建StreamWriter的实例,您只需创建一个MyStreamWriter:
var sw = new MyStreamWriter(socketStream);
我使用Trace类来记录消息,而不是将其写入另一个流。像log4net这样的组件允许记录对Trace(或Debug)类的所有调用,并将其路由到日志文件/SQL Server/任何东西。这样你就有了一个更加灵活的解决方案。
作为替代方案,您可以自己实现TraceListener,它可以简单地将所有输出添加到TextBox中。
谢谢你,Henk。好主意
但我有点改变。我只需要日志功能,所以我添加了日志概念。它基于你的拆分器想法。
可对数流类
public class LoggableStream : Stream
{
private Stream _stream;
private Encoding _textEncoding;
public LoggableStream(Stream stream, Encoding textEncoding)
{
_stream = stream;
_textEncoding = textEncoding;
}
public override bool CanRead
{
get
{
return _stream.CanRead;
}
}
public override bool CanSeek
{
get
{
return _stream.CanSeek;
}
}
public override bool CanWrite
{
get
{
return _stream.CanWrite;
}
}
public override long Length
{
get
{
return _stream.Length;
}
}
public override long Position
{
get
{
return _stream.Position;
}
set
{
_stream.Position = Position;
}
}
public override void Flush()
{
_stream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
var result = _stream.Read(buffer, offset, count);
try
{
var log = this._textEncoding.GetString(buffer, offset, count);
Trace.TraceInformation("READ : " + log);
}
catch (Exception ex)
{
Trace.TraceError(ex.ToString());
}
return result;
}
public override long Seek(long offset, SeekOrigin origin)
{
var result = _stream.Seek(offset, origin);
return result;
}
public override void SetLength(long value)
{
_stream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
_stream.Write(buffer, offset, count);
try
{
var log = this._textEncoding.GetString(buffer, offset, count);
Trace.TraceInformation("WRIT : " + log);
}
catch (Exception ex)
{
Trace.TraceError(ex.ToString());
}
}
}
用法
using (var netStream = _controlClient.GetStream())
using (var sr = new StreamReader(new LoggableStream(netStream, Encoding.UTF8)))
using (var sw = new StreamWriter(new LoggableStream(netStream, Encoding.UTF8)))
{
var readLine = sr.ReadLine();
sw.WriteLine("hello");
}