I'm using Trace
in two of my projects, but I'm seeing different behavior in both:
The first project is a console application that I will convert to a service (at which point I will eliminate the Console traces), and I'm adding two Trace
listeners:
Trace.Listeners.Add(new TextWriterTraceListener(someFileStream));
Trace.Listeners.Add(new ConsoleTraceListener());
The project second one is a WinForm
application and I have a "ConsoleForm" which displays Trace information and the user can open and close it as they please. For that application I also add a Trace
listener:
Trace.Listeners.Add(new TextWriterTraceListener(
new TextBoxStreamWriter(new WriteToTextBox(OnTextBoxWrite))));
The TextBoxStreamWriter
is a custom class I've created that allows me to write to the ConsoleForm
's text box. In any case, here is the issue I'm having: the console application does not display any performance traces while the windows application does, here is an example of a performance trace:
API: Performance::OnCPUThread CPU Usage: [0%], Memory Usage: 59mb
I don't care to see the performance information, so I'm perfectly happy with how the console application is handling it, but I can't figure out how to get the same behavior for the windows application. Does anybody know why this is happening and how I can fix it?
Update
I'm not doing anything crazy, actually it's even simple... here is my code:
public partial class ConsoleForm : Form
{
public delegate void WriteToTextBox(string value);
public ConsoleForm()
{
InitializeComponent();
Trace.AutoFlush = true;
Trace.Listeners.Add(new TextWriterTraceListener(
new TextBoxStreamWriter(new WriteToTextBox(OnTextBoxWrite))));
}
private void ConsoleForm_Load(object sender, EventArgs e)
{
}
private void OnTextBoxWrite(string value)
{
if (!this.IsHandleCreated)
return;
if (this.InvokeRequired)
{
object[] parameters = { value };
BeginInvoke(new WriteToTextBox(OnTextBoxWrite), parameters);
}
else
{
// ConsoleBox is a simple multiline text box
ConsoleBox.AppendText(value);
}
}
private void ConsoleForm_Closing(object sender, FormClosingEventArgs e)
{
Trace.Flush();
if (e.CloseReason != CloseReason.FormOwnerClosing )
{
e.Cancel = true;
this.Hide();
}
}
}
My TextBoxStreamWriter
implements the TextWriter
interface... the only other difference is that the console app is .Net 4.0 while the GUI app is .Net 3.5, but I doubt that would make a difference. My TextBoxStreamWriter
implementation can be found here (pastie).