禁止第3方库控制台输出? [英] Suppress 3rd party library console output?

查看:70
本文介绍了禁止第3方库控制台输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用第3方库,该库恰好在控制台上喷了很多东西。像这样的代码...

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;
}

是否有一种简便的方法可以抑制ThirdPartyLibrary不必要的控制台输出?出于性能原因,解决方案中不能使用新的进程或线程。

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.

推荐答案

那么您可以使用 Console.SetOut TextWriter 的实现,该实现不会在任何地方编写:

Well you can use Console.SetOut to an implementation of TextWriter which doesn't write anywhere:

Console.SetOut(TextWriter.Null);

这会抑制所有 all 控制台输出。您始终可以维护对原始 Console.Out 编写器的引用,并将其用于自己的输出。

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.

这篇关于禁止第3方库控制台输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆