Sending output to a console window in Windows App

  • Thread starter Thread starter Paul Bowman
  • Start date Start date
P

Paul Bowman

Hi All

Java has a facility where you can send output to a DOS window using
System.out.println. This is useful while developing as you can see
what it going in your code.

Does .Net provide a similar facility? I notice that when a Windows app
runs there is an output window that contains messages from the
compiler etc. Is it possible to 'plug' into this?

Any help would be appreciated..

Regards

Paul Bowman
 
It does indeed

Console.WriteLine("Hello World");

If you run a windows program the output will be sent to the "Output"
window in Visual Studio
 
The framework has two good classes for what you are trying to accomplish in
the System.Diagnostics namespace:

o Trace - if you want to continue to produce trace statements in production
o Debug - if you only need debugging information while in development and do
not want that code executed in production.

Using them is simple. For example: "Trace.WriteLine("In Load_Form");" will
write "In Load_Form" to the output window in VS. In addition to uncondional
write statements there are also conditional methods, such as WriteLineIf,
Assert.

What is neat about the classes is that you can also hook up a listener to
record the tracing information in a text file or the event log. You could
control, if a permanent trace is written to file by a configuration setting
in the app.config file and thereby build tracing tools into production apps
that you can turn on and off.

To write all Trace.WriteLine message to a text file you only need the
following code:

System.IO.FileStream fsLog = new System.IO.FileStream("C:\Log\applog.txt",
System.IO.FileMode.OpenOrCreate);
TextWriterTraceListener listener = new TextWriterTraceListener(fsLog);
Trace.Listeners.Add(listener);

You might have to control how often the information is written to disk, you
pretty much have two choices for that. Either manually flushing or
autoflushing to disk. To manually flush the log out, use Trace.Flush() or
automate it with Trace.AutoFlush = true;

If you want more information about it I would recommend looking into the
MCAD/MCSD certification books. I really like them since for an experienced
programmer that is jumping into .NET they have a nice pace compared to the
21 day books.

Hope this helps

Robert Sentgerath
 
Back
Top