views:

330

answers:

2

Quite often I run into items in the .NET framework that have little or no code examples on how to use the features. On other occasions, there are plenty of examples, but none of them seem to work as prescribed.

Case in point: The System.Diagnostics.ConsoleTraceListener class.

From everything I've read and every code example I've seen, when I'm doing something like the following:

ConsoleTraceListner listener = new ConsoleTraceListener();
listener.WriteLine("Yo");

...I should see a console window pop up with "Yo". However instead of that expected output, it's writing "Yo" to the Visual Studio Output where one would see normal Trace/Debug messages.

I've attempted every itteration of examples I've Googled for including config file setup of the appropriate listener, adding the listener to the Trace.Listeners collection, custom ConsoleTraceListener types, etc. I've even just compiled to a Release version and ran the executable (which does absolutely nothing).

What the heck am I missing?

+2  A: 

The Visual Studio output window is mapped to STDOUT. You need to make sure that your project is a console project if you want to write the stuff out to the actual console. You can still create Windows from a Console application, but you cannot create a console correctly without being a Console application because Windows won't assign you one.

Orion Adrian
That kinda doesn't make sense. What's the point of having a ConsoleTraceListener inside of a Console app? I could just write "Console.WriteLine()" all day long from a Console app. I thought the whole purpose of having a ConsoleTraceListener was to have tracing via Console from anywhere one chooses?
Boydski
See my answer - it wouldn't all have fit into a comment :)
Jon Skeet
+3  A: 

Answering your comment to Orion Adrian's answer...

The point of a ConsoleTraceListenter or any TraceListener isn't to call it directly - it's to add it to the trace listener collection so that the rest of your code just has to call Trace.Write etc. For instance:

using System;
using System.Diagnostics;

class Test
{
    static void Main()
    {
        ConsoleTraceListener listener = new ConsoleTraceListener();
        Trace.Listeners.Add(listener);

        DoSomething();

        Trace.Flush();
    }

    static void DoSomething()
    {
        DoSomethingElse();
    }

    static void DoSomethingElse()
    {
        // We don't want to have to pass all our logging
        // baggage down this far
        Trace.WriteLine("In DoSomethingElse");
    }
}

Compile that with TRACE defined, and you've got simple logging to the console. Personally I prefer logging with a bit more "oomph" such as log4net, but tracing is a good start.

Jon Skeet
Also in the case above you could have multiple Trace listeners, one for ConsoleTraceListener and maybe one for EventLogTraceListener.
eschneider
@Jon, thanx much for your input. But I've written what you wrote almost verbatim. My comment states it above: "I've attempted every itteration of examples I've Googled for including...adding the listener to the Trace.Listeners collection...". But it still doesn't work the way I'm expecting it to. I've yet to stumble upon the right combination to actually render a console with text in it.
Boydski
@Boydski: I was only answering your comment to Orion as to the benefits of using a trace listener in the first place.
Jon Skeet