You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public abstract partial class TextWriter
{
+ public static TextWriter Aggregate(ReadOnlySpan<TextWriter> writers);
}
Open questions:
Is this the right name?
Should there be an optional parameter for leaveOpen?
Do we need an array-based overload? params?
Background and motivation
It's common in simple command-line apps/tools to want to write output to multiple places, e.g. to the console and to a file.
API Proposal
namespaceSystem.IOpublicsealedclassTeeTextWriter:TextWriter{publicTeeTextWriter(paramsTextWriter[]writers);...// overrides of all TextWriter methods}
API Usage
usingStreamWriterfileWriter=newStreamWriter("...");varwriter=newTeeTextWriter(fileWriter,Console.Out);foreach(stringsinstrings)writer.WriteLine($"The value is {s}");
Alternative Designs
No response
Risks
No response
Reacted by Günther Foidl
Reacted by Jeremy Koritzinsky, startewho, Anton and Paulus Pärssinen
I was just basing on that on the Unix tee command. There are other options, though. Off the top of my head, MultiWriter, MultiTextWriter, PassthroughWriter, WrappingWriter, ... probably some others. For reference, there's also an open issue about a TeeReader.
... The most common use for this would normally be logging, though, I would imagine?
Not for me. For me it's usually that I'm writing a little tool that's generating something, and I want to save that out to a file, but I also want to see what it's generating, so I want a writer that writes both to a file and to the console. Basically the canonical use of tee.
The most common use for this would normally be logging, though
Imagine a CLI for calculations. You want output some stuff on the console, other stuff like results also on the console but in addition to a text-file. So plain tee won't work for this, as everything is written to the text-file.
With such a combined TextWriter you can be more specific what output goes where w/o manual doubling of the code.
I'd be fine with that approach. In theory, a concrete type would be more flexible in that it would enable retrieving a list of wrapped writers and dynamically adding/removing writers, should that be desired, but I don't personally have a need for that.
Should there be an optional parameter for leaveOpen?
How does it work when leaveOpen is true? I guess it means aggregateWriter.Dispose calls Flush and closes writing but doesn't dispose underlying writers. Do we need that?
Aggregate(ReadOnlySpan<TextWriter> writers) Do we need an array-based overload? params?
I think params is quite useful for this API.
Question: Is there some principles that why makes an API array-based only, ReadOnlySpan-based only, or both?
I guess it means aggregateWriter.Dispose calls Flush and closes writing but doesn't dispose underlying writers. Do we need that?
Right. That's the question.
Is there some principles that why makes an API array-based only, ReadOnlySpan-based only, or both?
Whether the API is expected to be used in a language with limited span support, eg VB.
Up until now, params has also only been limited to arrays. But a) that's changing, and b) params is also of limited value now that we have collection expressions.
We stuck with the static method alternative, but renamed Aggregate to CreateBroadcasting (during the meeting we'd discussed Broadcaster, but after subsequent discussion led to Broadcasting)
And converted it to a params-array method because of concern from non-C# callers.
@bartonjs If similar functionality for Streams land will it get the same / similar name? Will "Broadcaster" be a new general term for such functionality?
Reacted by David Cantú
ghost added
in-prThere is an active PR which will close this issue when it is merged
Activity
adamsitnik commentedon Oct 17, 2023
As a non-native speaker I had to check the dictionary to see what
Teemeans. Do you have any other ideas for the name?stephentoub commentedon Oct 17, 2023
I was just basing on that on the Unix
teecommand. There are other options, though. Off the top of my head, MultiWriter, MultiTextWriter, PassthroughWriter, WrappingWriter, ... probably some others. For reference, there's also an open issue about a TeeReader.MichalPetryka commentedon Oct 17, 2023
CombiningWriter,DelegatingWritermaybe?jkoritzinsky commentedon Oct 17, 2023
We have a specialized version of this type in the test infrastructure here. I'd happily switch to an in-box version if this API were to be approved.
ilmax commentedon Oct 17, 2023
CompositeTextWriter may be another viable name
paulbartrum commentedon Oct 17, 2023
SplittingTextWriter? (The input text is split into two output streams.) Edit: Or DuplicatingTextWriter.
Clockwork-Muse commentedon Oct 18, 2023
... The most common use for this would normally be logging, though, I would imagine? But logging implementors already provides this behavior...
stephentoub commentedon Oct 18, 2023
Not for me. For me it's usually that I'm writing a little tool that's generating something, and I want to save that out to a file, but I also want to see what it's generating, so I want a writer that writes both to a file and to the console. Basically the canonical use of
tee.skyoxZ commentedon Oct 18, 2023
I think it's unnecessary to make the class public. A static method in
TextWriteris enough:gfoidl commentedon Oct 19, 2023
Imagine a CLI for calculations. You want output some stuff on the console, other stuff like results also on the console but in addition to a text-file. So plain
teewon't work for this, as everything is written to the text-file.With such a combined TextWriter you can be more specific what output goes where w/o manual doubling of the code.
stephentoub commentedon Oct 19, 2023
I'd be fine with that approach. In theory, a concrete type would be more flexible in that it would enable retrieving a list of wrapped writers and dynamically adding/removing writers, should that be desired, but I don't personally have a need for that.
1 remaining item
stephentoub commentedon Jan 3, 2024
It might also be useful to have a Stream-based solution, but it wouldn't be instead of. It's very common to only have a TextWriter and not a Stream.
[-][API Proposal]: TeeTextWriter[/-][+][API Proposal]: TextWriter.Aggregate[/+]skyoxZ commentedon Jan 3, 2024
How does it work when
leaveOpenis true? I guess it meansaggregateWriter.DisposecallsFlushand closes writing but doesn't dispose underlying writers. Do we need that?I think
paramsis quite useful for this API.Question: Is there some principles that why makes an API array-based only,
ReadOnlySpan-based only, or both?stephentoub commentedon Jan 3, 2024
Right. That's the question.
Whether the API is expected to be used in a language with limited span support, eg VB.
Up until now, params has also only been limited to arrays. But a) that's changing, and b) params is also of limited value now that we have collection expressions.
bartonjs commentedon Jan 9, 2024
Video
En3Tho commentedon Jan 9, 2024
@bartonjs If similar functionality for Streams land will it get the same / similar name? Will "Broadcaster" be a new general term for such functionality?