Class TextWriterStreamHandler
A stream handler that writes to a TextWriter.
Useful for writing streaming output to files, StringWriter, or other text-based destinations.
public sealed class TextWriterStreamHandler : IAgentStreamHandler, IDisposable
- Inheritance
-
TextWriterStreamHandler
- Implements
- Inherited Members
Examples
// Write to file
using var writer = new StreamWriter("output.txt");
using var handler = new TextWriterStreamHandler(writer);
await agent.RunStreamingAsync("Hello", handler);
// Capture to string
var sw = new StringWriter();
using var handler = new TextWriterStreamHandler(sw);
await agent.RunStreamingAsync("Hello", handler);
string result = sw.ToString();
Constructors
- TextWriterStreamHandler(TextWriter, bool)
Initializes a new instance of the TextWriterStreamHandler class.
Properties
- AutoFlush
Gets or sets whether to flush after each token.
- IncludeStatus
Gets or sets whether to include status messages.
- IncludeThinking
Gets or sets whether to include thinking tokens in output.
- IncludeToolCalls
Gets or sets whether to include tool call notifications.
- StatusPrefix
Gets or sets the prefix for status tokens.
- ThinkingPrefix
Gets or sets the prefix for thinking tokens.
- ToolCallPrefix
Gets or sets the prefix for tool call tokens.
Methods
- Dispose()
Disposes the handler.
- OnCompleteAsync(AgentExecutionResult, CancellationToken)
Called when streaming completes.
- OnErrorAsync(Exception, CancellationToken)
Called when an error occurs during streaming.
- OnStartAsync(Agent, string, CancellationToken)
Called when streaming starts.
- OnTokenAsync(AgentStreamToken, CancellationToken)
Called when a new token is received.
- ToFile(string, bool)
Creates a handler that writes to a file.
- ToString(out StringWriter)
Creates a handler that writes to a StringWriter.