Skip to content

[API Proposal]: TextWriter.Aggregate #93623

Closed

Description

@stephentoub

UPDATED PROPOSAL 1/3/2024:

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

namespace System.IO

public sealed class TeeTextWriter : TextWriter
{
    public TeeTextWriter(params TextWriter[] writers);
    ... // overrides of all TextWriter methods
}

API Usage

using StreamWriter fileWriter = new StreamWriter("...");
var writer = new TeeTextWriter(fileWriter, Console.Out);
foreach (string s in strings)
    writer.WriteLine($"The value is {s}");

Alternative Designs

No response

Risks

No response

Activity

  1. added
    api-suggestion
    on Oct 17, 2023
  2. added this to the 9.0.0 milestone on Oct 17, 2023
  3. adamsitnik commented on Oct 17, 2023

    @adamsitnik
    Member

    As a non-native speaker I had to check the dictionary to see what Tee means. Do you have any other ideas for the name?

  4. stephentoub commented on Oct 17, 2023

    @stephentoub
    MemberAuthor

    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.

  5. MichalPetryka commented on Oct 17, 2023

    @MichalPetryka
    Contributor

    CombiningWriter, DelegatingWriter maybe?

  6. jkoritzinsky commented on Oct 17, 2023

    @jkoritzinsky
    Member

    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.

  7. ilmax commented on Oct 17, 2023

    @ilmax

    CompositeTextWriter may be another viable name

  8. paulbartrum commented on Oct 17, 2023

    @paulbartrum

    SplittingTextWriter? (The input text is split into two output streams.) Edit: Or DuplicatingTextWriter.

  9. Clockwork-Muse commented on Oct 18, 2023

    @Clockwork-Muse
    Contributor

    ... The most common use for this would normally be logging, though, I would imagine? But logging implementors already provides this behavior...

  10. stephentoub commented on Oct 18, 2023

    @stephentoub
    MemberAuthor

    ... 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.

  11. skyoxZ commented on Oct 18, 2023

    @skyoxZ
    Contributor

    I think it's unnecessary to make the class public. A static method in TextWriter is enough:

    public abstract partial class TextWriter
    {
        public static TextWriter Combine(params TextWriter[] writers);
    }
  12. gfoidl commented on Oct 19, 2023

    @gfoidl
    Member

    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.

  13. stephentoub commented on Oct 19, 2023

    @stephentoub
    MemberAuthor

    A static method

    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.

  14. 1 remaining item

  15. self-assigned this
    on Dec 26, 2023
  16. stephentoub commented on Jan 3, 2024

    @stephentoub
    MemberAuthor

    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.

  17. added
    api-ready-for-review
    and removed
    api-suggestion
    on Jan 3, 2024
  18. changed the title [-][API Proposal]: TeeTextWriter[/-] [+][API Proposal]: TextWriter.Aggregate[/+] on Jan 3, 2024
  19. skyoxZ commented on Jan 3, 2024

    @skyoxZ
    Contributor

    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?

  20. stephentoub commented on Jan 3, 2024

    @stephentoub
    MemberAuthor

    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.

  21. bartonjs commented on Jan 9, 2024

    @bartonjs
    Member

    Video

    • 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.
    public abstract partial class TextWriter
    {
        public static TextWriter CreateBroadcasting(params TextWriter[] writers);
    }
  22. added
    api-approved
    and removed
    api-ready-for-review
    on Jan 9, 2024
  23. En3Tho commented on Jan 9, 2024

    @En3Tho
    Contributor

    @bartonjs If similar functionality for Streams land will it get the same / similar name? Will "Broadcaster" be a new general term for such functionality?

  24. ghost added
    in-pr
    on Jan 10, 2024
  25. ghost removed
    in-pr
    on Jan 22, 2024
  26. locked and limited conversation to collaborators on Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.IO

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions