Reputation: 1308
Guten Tag!
The problem is: I have some code which prints some text messages via Console
class on terminal (command line window). I need this info to be placed in two 'containers' - terminal and text file.
Is there a way of adding output stream to Console
class in order to make it output data not only on terminal?
It'd be grate if I wont need to change existing code too much (there are a lot of places where Console.Write()
and Console.WriteLine()
are used).
Upvotes: 4
Views: 7828
Reputation: 11
I can't get the post by iamkrillin to work.
But the following does work:
class Program
{
static void Main(string[] args)
{
ConsoleFileOutput cf = new ConsoleFileOutput("Output.txt", Console.Out);
Console.SetOut(cf);
Console.WriteLine("0ne");
Console.WriteLine("two");
Console.WriteLine("three");
cf.Close();
}
public class ConsoleFileOutput : TextWriter
{
#region Fields
private Encoding encoding = Encoding.UTF8;
private StreamWriter writer;
private TextWriter console;
#endregion
#region Properties
public override Encoding Encoding
{
get
{
return encoding;
}
}
#endregion
#region Constructors
public ConsoleFileOutput(string filePath, TextWriter console, Encoding encoding = null)
{
if (encoding != null)
{
this.encoding = encoding;
}
this.console = console;
this.writer = new StreamWriter(filePath, false, this.encoding);
this.writer.AutoFlush = true;
}
#endregion
#region Overrides
public override void Write(string value)
{
Console.SetOut(console);
Console.Write(value);
Console.SetOut(this);
this.writer.Write(value);
}
public override void WriteLine(string value)
{
Console.SetOut(console);
Console.WriteLine(value);
this.writer.WriteLine(value);
Console.SetOut(this);
}
public override void Flush()
{
this.writer.Flush();
}
public override void Close()
{
this.writer.Close();
}
#endregion
#region IDisposable
new public void Dispose()
{
this.writer.Flush();
this.writer.Close();
this.writer.Dispose();
base.Dispose();
}
#endregion
}
}
Upvotes: 1
Reputation: 93
You should use StreamWriter
. It requires minimum additional code.
Follow this example and it will work.
you need to type " using System.IO" in order for this code to work. Don't forget to write it.
the "YourTextFile.txt" needs the .txt.
In order to see the results in you text file, you need to go to the actual folder in your computer, and open the Text file from there. The text file in Visual Studio will appear empty.
( e.g. c:/documents/visualstudio/projects/ConsoleApplication1/bin/debug/YourTextFile.txt)
Solution First Create a text file YourTextFile.txt in Visual Studio. Then in the Solution Explorer menu to the right, click on YourTextFile in order to see the Properties below the Solution Explorer. Change the property "Copy to Output Directory" from "Do Not Copy" to "Copy Always". (This is an absolute must)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Answer
{
static void Main(string[] args)
{
string text = "This text will appear on your console and be saved in the txt file";
Console.WriteLine(text);
StreamWriter saveText = new StreamWriter("YourTextFile.txt");
saveText.WriteLine(text);
saveText.Close();
Console.ReadLine();
}
}
}
This should do the job. I don't know what your output in the console looks like, but if it is not just one line, and there are multiple Console.Writelines giving output to the console, then you can use while
and if
, to check every line of the console and if it is not null
to write it out. e.g. while (line != null)
followed byif (line != null)
(assuming you set "line" to equal the output in your console)
Upvotes: 0
Reputation: 6876
This is not a full implementation but it should be enough to get you started down the path you seek.
class Program
{
static void Main(string[] args)
{
DualOut.Init();
Console.WriteLine("Hello");
Console.ReadLine();
}
}
public static class DualOut
{
private static TextWriter _current;
private class OutputWriter : TextWriter
{
public override Encoding Encoding
{
get
{
return _current.Encoding;
}
}
public override void WriteLine(string value)
{
_current.WriteLine(value);
File.WriteAllLines("Output.txt", new string[] { value });
}
}
public static void Init()
{
_current = Console.Out;
Console.SetOut(new OutputWriter());
}
}
If you run this code you will see that "Hello" is printed to both the console and to the file "Output.txt"
Upvotes: 8
Reputation: 464
You can't do it without changing your code. Simply create another class, for example "MyConsole" and add static methods "Write()" and "WriteLine()" to it. Internally call Console.Write()
and Console.WriteLine()
methods from your created methods. Also call methods that write text to a file from your methods. Then use your MyConsole
class instead of Console
for output data. Visual Studio Find/Replace feature will help you changing your code.
Upvotes: 0