Pergunta

For some reason unknown to me, the developer of a library I am using has decided to Console.WriteLine tons of information whenever a method is called within the library, essentially explaining every step of what is going on.

This is fine, but during development I don't need to be seeing this all of the time, so my question is can I suppress Console.WriteLine from being used when I don't want it to be? I still wish to be able to see my own Console.WriteLines, but not the ones within the Library.

Goes without saying I do not have source for the library.

Regards,

AK

Foi útil?

Solução

You can use Console.SetOut to redirect Console.Out to something of your choosing (e.g. something that discards all input) before calling into that library and then do the same to set the original value back. It's going to be awkward to do that all the time though.

To make things a bit more manageable, you can create another class that implements IDisposable and use it like this:

using(new StandardOutSink()) {
    // call into the third party library
}

The constructor of StandardOutSink (which you need to write) would redirect the output stream to a specially-crafted TextWriter (which you also need to write) and then the Dispose method would set it back to the original stream.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top