VB.Net Redirect Console Output to a text file

deskcheck1-0579 411 Reputation points


I have a very long running console application that uses multi-threading. Because all threads access the same log file, it becomes slow as multiple threads are active. I have implemented a synchronization so the threads wait for each other before they can write to the log file in order to avoid deadlocks. But this is slowing down the whole process, especially when there are hundreds of threads.

The console screen messages are important for debugging and getting general information (e.g, time elapsed for each thread to finish processing its tasks, errors occurring at what line number of the codes).

In order not to write often to the log file and create more traffic chaos, I was wondering if it would be possible to copy the entire console messages to a text file AFTER all the tasks are done?

I mean, the console is just sitting there with all this great information and I can't capture them to a text file for review or sharing with my boss.

Any ideas? Appreciate any help.

.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,099 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 52,311 Reputation points

    You should have a dedicated thread to write to the console. Just pass the the logging info to thread via a queue from each other thread.

    Or just use the .net ilogger for console.

0 additional answers

Sort by: Most helpful