I want to exit the program if I see some key words apprearing in Console.Out. This is because we use a third party DLL which has a problem that when it encounters some particular exception it never exits.
The only sulotion to us seems to be monitoring the log that is populated back to console.Out. And base on the log on console.out, the host application can edecide what to do when such exception are encountered.
Someone told me that I can use trace listener… but I am not sure about that.
what do you guys think?
The
Consoleclass provides theSetOutmethod which can be used to write output to a custom stream. For example, you could stream to a StringBuilder and monitor changes, or write a custom stream implementation that watches for the keywords.For example, here is a
KeywordWatcherStreamWrapperclass that watches for specified keyword, and raises an event for all listeners whenever the keyword is seen:Sample usage:
On the second
Writestatement which contains the entire keyword, the event will be raised and thus the exception will be thrown. Note also that this silently wraps the underlying stream and still writes to it, so console output is still generated as normal.Sample output: