views:

98

answers:

2

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

+1  A: 

Just adding a TraceListener won't cause that message to be logged.

Can you provide more details about what your app is doing.

Try testing your TraceListener in a new simple WinForm app, I wouldn't expect you to see those messages. If you do, there is something in your listener implementation..

Les
I've updated my question with the code of the form (it's very simple) and I've posted my TextBoxStreamWriter into a pastie: http://www.pastie.org/1185751 but I don't see anything out of the ordinary there.
Lirik
Another thing... I tried creating a new GUI project from scratch, but I didn't see the performance traces in there either. Now I'm actually suspecting that one of the 3rd party DLLs that I'm loading is spitting out the performance traces...
Lirik
@Link - Trend Micro tells me not to go to pastie.org, so I'll forego looking at that code. Your 3rd party DLLs are likely the culprit. Do they have .config file entries you can examine? Maybe there is a configuration parameter you can set to turn on/off their traces.
Les
@Les, it is definitely the 3rd party DLL, I just ran the vendor's sample application and it had the same trace outputs. Your answer prompted me to find that out, so I feel warranted to give you the points :).
Lirik
A: 

Thanks to Les, I forced myself to delve a little deeper into the "issue" and realized that a 3rd party DLL was causing the performance logs. There seems to be nothing inherent to C# or the Trace class that would display trace messages from anywhere else but the programmer's code or referenced libraries.

Lirik
If you work hard, you can make your C# code display debug trace statements from other running applications. See http://www.codeproject.com/KB/trace/DbMonNET.aspx but I digress.
Les