M
michael sorens
I want to send the output of a TraceSource to a TextBox on a WinForm.
I came up with this rudimentary approach but it does not seem terribly
efficient to close the StringWriter and get a new one each time I want to
output a chunk of tracing info. Further, I have to "manually" move the chunks
of data rather than having a pipeline, which would let each trace output line
flow into the TextBox as it occurs.
So I would welcome comments on two fronts:
(1) Is there a way to make the whole thing more automatic? That is, instead
of explicitly moving data as I have done, is there a way to set up a pipeline
connecting the output of the StringWriter to the TextBox?
(2) Failing the above, is there a cleaner/better way to implement what I
have shown below?
// initialize
strWriter = new StringWriter();
mySource.Listeners.Add(
new TextWriterTraceListener(strWriter, "TextBox"));
while (more to do) {
...do some output...
// process collected trace output into TextBox
StringReader strReader = new StringReader(strWriter.ToString());
string str = strReader.ReadLine();
while (str != null)
{
str = strReader.ReadLine();
diagTextBox.Text += str + Environment.NewLine;
}
// reset for next pass
strWriter.Close();
strWriter = new StringWriter();
((TextWriterTraceListener)mySource.Listeners["TextBox"]).Writer =
strWriter;
}
I came up with this rudimentary approach but it does not seem terribly
efficient to close the StringWriter and get a new one each time I want to
output a chunk of tracing info. Further, I have to "manually" move the chunks
of data rather than having a pipeline, which would let each trace output line
flow into the TextBox as it occurs.
So I would welcome comments on two fronts:
(1) Is there a way to make the whole thing more automatic? That is, instead
of explicitly moving data as I have done, is there a way to set up a pipeline
connecting the output of the StringWriter to the TextBox?
(2) Failing the above, is there a cleaner/better way to implement what I
have shown below?
// initialize
strWriter = new StringWriter();
mySource.Listeners.Add(
new TextWriterTraceListener(strWriter, "TextBox"));
while (more to do) {
...do some output...
// process collected trace output into TextBox
StringReader strReader = new StringReader(strWriter.ToString());
string str = strReader.ReadLine();
while (str != null)
{
str = strReader.ReadLine();
diagTextBox.Text += str + Environment.NewLine;
}
// reset for next pass
strWriter.Close();
strWriter = new StringWriter();
((TextWriterTraceListener)mySource.Listeners["TextBox"]).Writer =
strWriter;
}