Reputation:
In my application I have a lot of Console#WriteLine
or Console#Write calls
. This are so many, that the application is really slower than necessary.
So I'm looking for an easy way to remove or at least disable all Console#WriteLine and Console#Write
calls.
What's the easiest way to achieve that?
Upvotes: 10
Views: 11448
Reputation: 2069
To switch off Console.WriteLine() output in runtime temporarily use
// backup the previous output handler connected to Console
TextWriter backupOut = Console.Out;
// activate a null handle
Console.SetOut(TextWriter.Null);
// this console output will be invisible
Console.WriteLine("Hidden output on Console.");
// restore the previous handle
Console.SetOut(backupOut);
// this console output will be visible
Console.WriteLine("Showing output on Console.");
Similarly, you can disable program Debug.WriteLine() output in runtime, also while compiling in Debug mode, by using a dummy TextWriterTraceListener, like so
using System.Diagnostics;
using System.IO;
//...
public class MyDummyListener: TextWriterTraceListener
{ }
// ......
[STAThread]
static void Main()
{
// Back up the old one
DefaultTraceListener[] debuglisteners = {(DefaultTraceListener) Debug.Listeners[0]};
// Plug in dummy listener
TextWriterTraceListener[] dummylisteners = {new MyDummyListener()};
Debug.Listeners.Clear();
Debug.Listeners.AddRange(dummylisteners);
// this is an invisible debug message
Debug.WriteLine("This one is invisible");
// ..
// to activate output again, plug in the previous listener
Debug.Listeners.Clear();
Debug.Listeners.AddRange(debuglisteners);
Debug.WriteLine("This one is visible again");
// ....
}
Note: I tested above code in .NET Framework 4.7.2 in Debug mode, using the Winforms platform.
Upvotes: 4
Reputation: 417
Here's a even quicker quick fix I implemented.
...
static int Main(String[] args)
{
...
#if !DEBUG
Console.SetOut(TextWriter.Null);
Console.SetError(TextWriter.Null);
#endif
...
}
HTH
Upvotes: 30
Reputation: 273591
If they are non-essential (logging) then you should have used System.Diagnostics.Debug.Print()
to start with.
Luckily WriteLine()
is compatible with Debug.Print()
so that's an easy S&R. And fix some usings maybe.
Replacing Console.Write()
might be a little trickier.
To be complete: The Debug.Print() statements can be turned on/off with a checkbox in project|Properties.
Upvotes: 3
Reputation: 134065
In Visual Studio, use the Tools|Macros|Record temporary macro
option to record a macro that does a Find of "Console.Write", and deletes the line. That is:
Ctrl+F to find "Console.Write" then Ctrl+L to delete the line.
Save the macro and then run it against every file in the project that has the offending lines.
Should take about two minutes.
I would suggest, however, that you back up your source first. Just in case.
Upvotes: 1
Reputation: 52351
Use a simple, non regex, Find/Replace dialog, and replace every Console.WriteLine(
by Debug.WriteLine(
.
You can then keep the possibility to track what was previously outputted directly to the console window, still optimizing performance in release mode.
Upvotes: 1
Reputation: 7715
Maybe Find&Replace function in any code editor? For example find all
Console.WriteLine
and replace with
//Console.WriteLine
Upvotes: 7