Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] C# console redirect

Started by Kaze Sep 29, 2008 at 11:21 PM 5 replies 2.6k views
Original Post
Kaze
Kaze
Is their anyway I can redirect Console.Write() messages to a form. Their are about a dozen tutorials on how to redirect it to a file but I want to be able to manually read what gets written. I tried using a StringWriter class but thats a very messy solution and I need to constantly check for updates. I would just make my own logging system but I also need to get console messages from IronPython.
TheTroll
TheTroll
This is pretty easy if you take the time to wade through the documentation.

First we have Console.SetOut. That sets the Console output to a different TextWriter class. So how does that help us? Well make your own.

So make a new Class CustomTextWriter class that inherits from TextWriter. Make a private member to the control that you want to send the text to. Make a property to set the control.

In the CustomTextWriter override the Write method, setting the text of the control to the text sent to the Write method, and make sure you test to see if the control is null before you try to output to the form. You will also need to overrride Encoding.Get because it is abstract and must be overridden.

Now at the start of your app, create a CustomTextWriter cWriter. Then call Console.SetOut(cWriter).

From then on when you call Console.Write, it will output into your form control.

Tada.

theTroll
Kaze
Kaze
I was hoping to avoid that because the write and write line methods have like 50 overrides but I might do that anyway.
SiCrane
SiCrane
Well, what do you actually want to do when you say "manually read what gets written"? If we had a better description of what you're trying to do, we might be able to think of a better way to do it.
TheTroll
TheTroll
It is not really that hard.

Make a function CustomFormWrite that takes a string and does what you need it to do.

Next overrided the functions, in the functions call CustomFormWrite(value.ToString(); then let them also do their call to the base.Write or base.WriteLine.

You should be able to have it don't in about 30 minutes.

theTroll

P.S. SirCrane, how long have you been helping people here? You mean that by now you haven't learned to read intent no matter what they write? In this case he actually used proper grammar and spelling, and had more then 10 words to explain what he was looking for.

This is what I normally expect.
Quote:

anyways I can redirect messages to a form dozen tutorials on redirect to a file I want to manually read what gets written.

plz post code so I can putz it in my proj.


Yeah, it has been one of those days. Hopefully people will find it as funny as I did.
capn_midnight
capn_midnight
I did this for my &#106avascript development environment project:

using System;using System.IO;using System.Text;namespace JScript_Runtime_Environment{	class ScriptHost : TextWriter	{		public delegate void UpdateOutputDelegate(string line);		public event UpdateOutputDelegate OnUpdateOutput;<lè sníppe/>				public void ExecuteFullScript(string script)		{			Console.SetOut(this);<lè build lè script, lè compile, and lè run it />		}		public override void WriteLine(string line)		{			this.OnUpdateOutput(string.Format("{0}\r\n", line));		}		public override void Write(string line)		{			this.OnUpdateOutput(line);		}<lè sníppe again/>	}	}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.