I need to call a 3rd party library that happens to spew a bunch of stuff to the console. The code simply like this...
int MyMethod(int a)
{
int b = ThirdPartyLibrary.Transform(a); // spews unwanted console output
return b;
}
Is there an easy way to suppress the unwanted console output from ThirdPartyLibrary? For performance reasons, new processes or threads cannot be used in the solution.
Here's one way to do it (which also usually covers managed C++ applications that you P/Invoke from C# or otherwise):
This class can be called as follows:
This will have an impact. Any application logic that tries to use the console from another thread during the time you are
using
theOutputSink
will not function correctly to write to the standard output, standard error, console output, or console error.Well you can use
Console.SetOut
to an implementation ofTextWriter
which doesn't write anywhere:That will suppress all console output though. You could always maintain a reference to the original
Console.Out
writer and use that for your own output.