Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8008285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:05:22+00:00 2026-06-04T18:05:22+00:00

all I’ve a library, not my, so that then it can’t be modified. This

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:05:23+00:00Added an answer on June 4, 2026 at 6:05 pm

    What you’re looking to do isn’t easy to do. Console.WriteLine() et al are static because 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 TextWriter that wraps multiple output files, and set that as the standard output. Then, have each thread register itself with your TextWriter, 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() for strings), and there are likely other bugs present as well.

    public class ThreadAwareStreamWriter : TextWriter
    {
        private ConcurrentDictionary<int, TextWriter> threadWriterMap;
        private TextWriter defaultWriter;
    
        public ThreadAwareStreamWriter()
        {
            this.threadWriterMap = new ConcurrentDictionary<int, TextWriter>();
            this.defaultWriter = Console.Out;
        }
    
        public TextWriter RegisterThreadWriter(TextWriter threadWriter)
        {
            int threadId = Thread.CurrentThread.ManagedThreadId;
    
            TextWriter oldWriter;
            this.threadWriterMap.TryGetValue(threadId, out oldWriter);
            this.threadWriterMap[threadId] = threadWriter;
    
            return oldWriter;
        }
    
        public void DeregisterThread()
        {
            TextWriter threadWriter;
            if (this.threadWriterMap.TryRemove(Thread.CurrentThread.ManagedThreadId, out threadWriter))
            {
                threadWriter.Close();
            }
        }
    
        public override Encoding Encoding
        {
            get 
            {
                TextWriter threadWriter;
                if (this.threadWriterMap.TryGetValue(Thread.CurrentThread.ManagedThreadId, out threadWriter))
                {
                    return threadWriter.Encoding;
                }
    
                return this.defaultWriter.Encoding;
            }
        }
    
        public override void WriteLine(string value)
        {
            TextWriter threadWriter;
            if (this.threadWriterMap.TryGetValue(Thread.CurrentThread.ManagedThreadId, out threadWriter))
            {
                threadWriter.WriteLine(value);
                return;
            }
    
            if (this.defaultWriter != null)
            {
                this.defaultWriter.WriteLine(value);
            }
        }
    }
    

    A sample program:

    public static void Main(string[] args)
    {
        ThreadAwareStreamWriter writer = new ThreadAwareStreamWriter();
        Console.SetOut(writer);
    
        Thread t = new Thread(o =>
        {
            ThreadAwareStreamWriter tWriter = (ThreadAwareStreamWriter)o;
            tWriter.RegisterThreadWriter(new StreamWriter(File.Open("test.txt", FileMode.Create, FileAccess.ReadWrite)));
            Console.WriteLine("Hello from another thread");
    
            tWriter.DeregisterThread();
        });
    
        t.Start(writer);
    
        Console.WriteLine("Hello");
        Console.ReadLine();
    }
    

    This will print out “Hello” to the console, and “Hello from another thread” to the file “test.txt”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All I have found about this is that there are inconsistencies among different web
All, I have some script tags that are not working in Wordpress. If I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
All, I currently have two projects that are under SourceSafe that I am unable
All, I have a DetailViewController that has its class set to UIControl and there
All, I have a form with a MultiValueField that almost works. It uses a
All, In my Lex file we recognize some operators as Tokens some of this
All, Can someone please help on how to include a directory in a path
All the guys probably will recommend that I read the follow previously question in
All, See the code below: function menu() { this.menuitem=[]; this.submenu=[]; this.menuitem[0] = $('div#sivname1'); this.menuitem[1]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.