Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7935511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:56:40+00:00 2026-06-03T21:56:40+00:00

I want to exit the program if I see some key words apprearing in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:56:42+00:00Added an answer on June 3, 2026 at 9:56 pm

    The Console class provides the SetOut method 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 KeywordWatcherStreamWrapper class that watches for specified keyword, and raises an event for all listeners whenever the keyword is seen:

    public class KeywordWatcherStreamWrapper : TextWriter
    {
        private TextWriter underlyingStream;
        private string keyword;
        public event EventHandler KeywordFound;
        public KeywordWatcherStreamWrapper(TextWriter underlyingStream, string keyword)
        {
            this.underlyingStream = underlyingStream;
            this.keyword = keyword;
        }
    
        public override Encoding Encoding
        {
            get { return this.underlyingStream.Encoding; }
        }
    
        public override void Write(string s)
        {
            this.underlyingStream.Write(s);
            if (s.Contains(keyword))
                if (KeywordFound != null)
                    KeywordFound(this, EventArgs.Empty);
        }
    
        public override void WriteLine(string s)
        {
            this.underlyingStream.WriteLine(s);
            if (s.Contains(keyword))
                if (KeywordFound != null)
                    KeywordFound(this, EventArgs.Empty);
        }
    }
    

    Sample usage:

    var kw = new KeywordWatcherStreamWrapper(Console.Out, "Hello");
    kw.KeywordFound += (s, e) => { throw new Exception("Keyword found!"); };
    
    try {   
        Console.SetOut(kw);
        Console.WriteLine("Testing");
        Console.WriteLine("Hel");
        Console.WriteLine("lo");
        Console.WriteLine("Hello");
        Console.WriteLine("Final");
    } catch (Exception ex) { Console.Write(ex.Message); }
    

    On the second Write statement 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:

    Testing
    Hel
    lo
    Hello
    Keyword found!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that I want to exit early under some condition: if
We do not want to use exit(0) to make the app close, we do
I want to know how and when can I use the exit() function like
I have some testcases for a program, so i want to start all of
This program want to read from a file. the content in the file is
i'm creating a program and i want to exit the program if i get
I want to get the exit value (returned by $? on a shell; usually
I want to execute an asynchronous callback when exit event is emitted. For example:
When in a FB app I want to press an html button and exit
Sorry to ask this as I thought I knew the answer, I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.