all
I’ve a library, not my, so that then it can’t be modified.
This library has two methods, let’s call them Method_1() and Method_2().
In these methods is called Console.WriteLine(“…”)
I’ve a class
public class CustomBackgroundWorker: BackgroundWorker
{
private readonly StringBuilder logBuilder;
public CustomBackgroundWorker ()
{
logBuilder = new StringBuilder ();
TextWriter outStream = new StringWriter (logBuilder);
Console.SetOut (outStream);
}
public string GetLogs ()
{
return logBuilder.ToString ();
}
}
It creates two instance of this class. Each instance execute method from the library. But output save only in one stream, because SetOut() method work globally.
Is it possible to redirect output from different thread to separate streams?
Thx
What you’re looking to do isn’t easy to do.
Console.WriteLine()et al arestaticbecause they are shared; any thread that calls those methods will write to the standard output stream.The cleanest way to split the output into separate files would be to create your own
TextWriterthat wraps multiple output files, and set that as the standard output. Then, have each thread register itself with yourTextWriter, specifying the desired output file. Whenever a call comes in to the writer, it looks at which thread made the call, and then writes to the appropriate file.This obviously requires some work to get right (since there are numerous race conditions and resource sharing issues a whatnot that could arise) but would allow you to do what you want.
Edit: Here’s a sample of what such a writer might look like, as well as how it might be used. This class is incomplete (e.g. it currently only supports
WriteLine()forstrings), and there are likely other bugs present as well.A sample program:
This will print out “Hello” to the console, and “Hello from another thread” to the file “test.txt”.