0

I have an API method WriteSerie() which writes on the console some data. My goal is to parse in real time the output of WriteSerie() such that if a particular condition happens in the output, the console writes another line defined by me. What are the methods to use for this purpose?

Afe
  • 167
  • 1
  • 10

2 Answers2

3

I think you want to make a general modification to your console's output, for example if a Serie goes to output, you want write a message before it. so your code might be such this:

class MyWriter : TextWriter
{
    private TextWriter originalOut;
    public MyWriter()
    {
        originalOut = Console.Out;
    }
    public override Encoding Encoding
    {
        get { return new System.Text.ASCIIEncoding(); }
    }
    public override void WriteLine(string message)
    {
        originalOut.WriteLine(CheckMySerie(message));
    }
    public override void Write(string message)
    {
        originalOut.Write(CheckMySerie(message));
    }
    private string CheckMySerie(string message)
    {
        if (message.Contains("MySerie"))
            return "My Serie has been found\n" + message;
        else
            return message;
    }

}

class Program
{
    static void Main(string[] args)
    {
        Console.SetOut(new MyWriter());
        Console.WriteLine("test 1 2 3");
        Console.WriteLine("test MySerie 2 3");
        Console.ReadKey();
    }
}
Sahar.H
  • 56
  • 2
-2

First Solution:

Console.WriteLine("Hello World");
FileStream fs = new FileStream("Test.txt", FileMode.Create);
// First, save the standard output.
TextWriter tmp = Console.Out;
StreamWriter sw = new StreamWriter(fs);
Console.SetOut(sw);
Console.WriteLine("Hello file");
Console.SetOut(tmp);
Console.WriteLine("Hello World");
sw.Close();

you could use any streamwriter.

Second solution:

public class ConsoleWriterEventArgs : EventArgs
    {
        public string Value { get; private set; }
        public ConsoleWriterEventArgs(string value)
        {
            Value = value;
        }
    }

    public class ConsoleWriter : TextWriter
    {
        public override Encoding Encoding { get { return Encoding.UTF8; } }

        public override void Write(string value)
        {
            if (WriteEvent != null) WriteEvent(this, new ConsoleWriterEventArgs(value));
            base.Write(value);
        }

        public override void WriteLine(string value)
        {
            if (WriteLineEvent != null) WriteLineEvent(this, new ConsoleWriterEventArgs(value));
            base.WriteLine(value);
        }

        public event EventHandler<ConsoleWriterEventArgs> WriteEvent;
        public event EventHandler<ConsoleWriterEventArgs> WriteLineEvent;
    }

Set Listener event:

 static void Main()
    {
        using (var consoleWriter = new ConsoleWriter())
        {
            consoleWriter.WriteEvent += consoleWriter_WriteEvent;
            consoleWriter.WriteLineEvent += consoleWriter_WriteLineEvent;

            Console.SetOut(consoleWriter);
        }
    }

you will receive every single line in your event

Ji Ra
  • 3,187
  • 1
  • 12
  • 17
  • 1
    I do not see anything wrong with copy/pasting code from another post, but you could at least add a link to the [original answer](http://stackoverflow.com/a/11911734/4840746)... – Sébastien Sevrin Jul 06 '15 at 10:35