-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: TextWriter.Aggregate #93623
Comments
Tagging subscribers to this area: @dotnet/area-system-io Issue DetailsBackground and motivationIt's common simple console apps to want to write output to multiple places, e.g. to the console and to a file. API Proposalnamespace System.Collections.Generic;
public sealed class TeeTextWriter : TextWriter
{
public TeeTextWriter(params TextWriter[] writers);
... // overrides of all TextWriter methods
} API Usageusing StreamWriter fileWriter = new StreamWriter("...");
var writer = new TeeTextWriter(fileWriter, Console.Out);
foreach (string s in strings)
writer.WriteLine($"The value is {s}"); Alternative DesignsNo response RisksNo response
|
As a non-native speaker I had to check the dictionary to see what |
I was just basing on that on the Unix |
|
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. |
CompositeTextWriter may be another viable name |
SplittingTextWriter? (The input text is split into two output streams.) Edit: Or DuplicatingTextWriter. |
... The most common use for this would normally be logging, though, I would imagine? But logging implementors already provides this behavior... |
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 |
I think it's unnecessary to make the class public. A static method in public abstract partial class TextWriter
{
public static TextWriter Combine(params TextWriter[] writers);
} |
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 |
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. |
Wouldn't it be more generic to implement this combination logic at the stream level ? |
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. |
How does it work when
I think |
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. |
public abstract partial class TextWriter
{
public static TextWriter CreateBroadcasting(params TextWriter[] writers);
} |
@bartonjs If similar functionality for Streams land will it get the same / similar name? Will "Broadcaster" be a new general term for such functionality? |
UPDATED PROPOSAL 1/3/2024:
public abstract partial class TextWriter { + public static TextWriter Aggregate(ReadOnlySpan<TextWriter> writers); }
Open questions:
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
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: