views:

495

answers:

2

Hi There, I have built a library that dumps most of its debug text using Console.WriteLine();

I am now I am the process of using the library in a Windows Forms Application, and still need access to the Console output. ( To Display in a List/RichText box )

I Noticed I can override the standard out of the console to a TextWriter, but how would I then get this data into the display .

I Tried doing something along the lines of

  public partial class Form1 : Form
  {
    Timer T;
    MemoryStream mem;
    StreamWriter writer; 


    public Form1()
    {

        InitializeComponent();
        mem = new MemoryStream(1000);
        writer = new StreamWriter(mem);
        Console.SetOut(writer);

        T = new Timer();
        T.Interval = 250; // yes this probally is to short.
        T.Tick += new EventHandler(T_Tick);
        T.Start();


        Console.WriteLine("output");
        Console.WriteLine("AnotherLine");
    }

    void T_Tick(object sender, EventArgs e)
    {
        string s = Encoding.Default.GetString(mem.ToArray());
        string[] Lines = s.Split(Environment.NewLine.ToCharArray());
        Output.Items.Clear(); // Output is a listbox 
        foreach (string str in Lines)
            Output.Items.Add(str);
    }
}

but to no avail. Any ideas?

Unneeded code removed.

+1  A: 

Rather than try to "capture" text sent to the console, I would create a new class that handles writing the output for you. Then that new class could write to the console, as well as anyplace else you want it to go.

If you're using .NET's Debug class, simply use two listeners: a ConsoleTraceListener and a TextWriterTraceListener.

To make your existing code work, add:

writer.Flush();

after your Console.WriteLine() calls.

Andy West
if i cant find a solution to this I will most likely end up doing it that way. but i would really rather do it via accessing the console out, ( Mainly as an ease of use thing, ) but also as an educational piece. its something I am sure I can find multiple uses for in the future so learning how to do this now will be rather handy.
Fusspawn
while I am not using the debug class ( its not all debug info ). I had forgotten about the existance of that Debug class i may just convert bits to use that.
Fusspawn
Ok. In that case to get your code to work, just add: writer.Flush();after your Console.WriteLine() calls.
Andy West
+3  A: 

Another, probably cleaner way to do this is to extend TextWriter with your own that logs to wherever you'd like it to.

Note: I have not tested this.

public class ListBoxWriter : TextWriter
{
    private ListBox list;
    private StringBuilder content = new StringBuilder();

    public ListBoxWriter(ListBox list)
    {
     this.list = list;
    }

    public override void Write(char value)
    {
     base.Write(value);
     content.Append(value);
     if (value == '\n')
     {
      list.Items.Add(content.ToString());
      content = new StringBuilder();
     }
    }

    public override Encoding Encoding
    {
     get { return System.Text.Encoding.UTF8; }
    }
}
Michael Petrotta
this does exactly what im after :D Thanks
Fusspawn