Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. A Console window for Trace.WriteLine

A Console window for Trace.WriteLine

Scheduled Pinned Locked Moved C#
tutorialdebuggingquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Account Removal
    wrote on last edited by
    #1

    Hi, I should like to write trace lines (using Trace.WriteLine or even Console.WriteLine) to a console window, but this time from a Class module. The class module does not create a Console (aka command line)window, so Console.WriteLine does not work. Neither could I find an example of specifying a (or creating a) console window for this kind of output. Anybody Any Idea how to do this? Tx Aad Aad Slingerland Zevenaar The Netherlands

    H D 2 Replies Last reply
    0
    • A Account Removal

      Hi, I should like to write trace lines (using Trace.WriteLine or even Console.WriteLine) to a console window, but this time from a Class module. The class module does not create a Console (aka command line)window, so Console.WriteLine does not work. Neither could I find an example of specifying a (or creating a) console window for this kind of output. Anybody Any Idea how to do this? Tx Aad Aad Slingerland Zevenaar The Netherlands

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Applications for either the console or windows subsystem won't display output for Trace.WriteLine unless you add the following lines of code before calling your first Trace.WriteLine (or Debug.WriteLine):

      Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));

      To display a console window for a Windows Forms applcation compiled for the window subsystem (because a Windows Forms application can be compiled for the console subsystem and work, plus write console data; ildasm.exe is compiled this way) you'd have to P/Invoke CreateConsoleScreenBuffer and write a TextWriter around that for use with the TextWriterTraceListener, or just write your own TraceListener derivative to do that. The easiest is just compile your Windows Forms application using /t:exe instead of /t:winexe so that you already have a console attached to the process. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

      A 1 Reply Last reply
      0
      • H Heath Stewart

        Applications for either the console or windows subsystem won't display output for Trace.WriteLine unless you add the following lines of code before calling your first Trace.WriteLine (or Debug.WriteLine):

        Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));

        To display a console window for a Windows Forms applcation compiled for the window subsystem (because a Windows Forms application can be compiled for the console subsystem and work, plus write console data; ildasm.exe is compiled this way) you'd have to P/Invoke CreateConsoleScreenBuffer and write a TextWriter around that for use with the TextWriterTraceListener, or just write your own TraceListener derivative to do that. The easiest is just compile your Windows Forms application using /t:exe instead of /t:winexe so that you already have a console attached to the process. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

        A Offline
        A Offline
        Account Removal
        wrote on last edited by
        #3

        Hello Heath, I give this a try first thing in the morning. Be back... Tx Aad Aad Slingerland Zevenaar The Netherlands

        1 Reply Last reply
        0
        • A Account Removal

          Hi, I should like to write trace lines (using Trace.WriteLine or even Console.WriteLine) to a console window, but this time from a Class module. The class module does not create a Console (aka command line)window, so Console.WriteLine does not work. Neither could I find an example of specifying a (or creating a) console window for this kind of output. Anybody Any Idea how to do this? Tx Aad Aad Slingerland Zevenaar The Netherlands

          D Offline
          D Offline
          dbetting
          wrote on last edited by
          #4

          Sound like you might be doing what I was doing. I kept writing Console harness apps to get the output to track what's going on while developing until I found out about System.Diagnostics.Debug.WriteLine(). If you're using .Net Studio, it will display the information in the Output window. I use it in both windows forms or special classes that have no GUI interface. Sure beat the heck out of the test harnesses.

          A 1 Reply Last reply
          0
          • D dbetting

            Sound like you might be doing what I was doing. I kept writing Console harness apps to get the output to track what's going on while developing until I found out about System.Diagnostics.Debug.WriteLine(). If you're using .Net Studio, it will display the information in the Output window. I use it in both windows forms or special classes that have no GUI interface. Sure beat the heck out of the test harnesses.

            A Offline
            A Offline
            Account Removal
            wrote on last edited by
            #5

            Working on the subject I found a even simpler way to create a console window from within a class module. It requires two p/invoke functions from kernel32 and than Console.WriteLine () will do! It goes like this... bool b = AllocConsole (); Console.WriteLine ("This..."); Console.WriteLine ("...That"); bool b = FreeConsole (); Aad Aad Slingerland Zevenaar The Netherlands

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups