Skip to content

[API Proposal]: TextWriter.Aggregate #93623

Closed
@stephentoub

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

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.IO

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions