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 8180375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:12:00+00:00 2026-06-07T00:12:00+00:00

I need to create a class with two properties: LogOutput ExceptionOutput These properties (Actions<>)

  • 0

I need to create a class with two properties:

  1. LogOutput
  2. ExceptionOutput

These properties (Actions<>) send a message or a exception depending on the target function. This target function is set via properties.

Currently, I have this functional code:

    public class Output
    {
        private Action<string> logOutput;
        private Action<Exception, string> exceptionOutput;

        public Action<string> LogOutput { set { this.logOutput = value; } get { return this.logOutput; } }
        public Action<Exception, string> ExceptionOutput { set { this.exceptionOutput = value; } get { return this.exceptionOutput; } }

        public Output() : this(null, null) { }

        public Output(Action<string> logAction, Action<Exception, string> exceptionAction) 
        {
            this.logOutput = logAction;
            this.exceptionOutput = exceptionAction;
        }


        public void WriteLogMessage(string format, params object[] args) 
        {
            if (this.logOutput != null)
                logOutput(string.Format(format, args));
        }

        public void WriteExceptionMessage(Exception ex, string format, params object[] args) 
        {
            if (this.exceptionOutput != null)
                exceptionOutput(ex, string.Format(format, args));
        }
    }

And this is my form code:

    private void MainForm_Load(object sender, EventArgs e)
    {
        // my Output object
        Output myOutput = new Output();

        // set properties
        myOutput.ExceptionOutput = this.WriteExceptionMessageToTextBox;
        myOutput.LogOutput = this.WriteLogMessageToTextBox;

        // test
        myOutput.WriteLogMessage("this is my log message to text box");
        myOutput.WriteExceptionMessage(new Exception("this is my exception"), "this is my exception message to text box");
    }

    private void WriteLogMessageToTextBox(string message)
    {
        // nothing to do here
        if (this.txtBox.IsDisposed)
            return;

        if (this.InvokeRequired)
        {
            BeginInvoke(new MethodInvoker(delegate() { WriteLogMessageToTextBox(message); }));
        }
        else 
        {
            // write to text box
            this.txtBox.AppendText(message + Environment.NewLine);
        }
    }

    private void WriteExceptionMessageToTextBox(Exception ex, string message)
    {
        // nothing to do here
        if (this.txtBox.IsDisposed)
            return;

        if (this.InvokeRequired)
        {
            BeginInvoke(new MethodInvoker(delegate() { WriteExceptionMessageToTextBox(ex, message); }));
        }
        else
        {
            string msg = "";
            msg += string.Format("Program:{0}", message);
            msg += string.Format("Message{0}", ex.Message);
            msg += string.Format("StackTrace:{0}", ex.StackTrace);
            msg += string.Format("Source:{0}", ex.Source);

            // write to text box
            this.txtBox.AppendText(msg + Environment.NewLine);
        }
    }

It is correct this model? There is another way to do this?

  • 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-07T00:12:01+00:00Added an answer on June 7, 2026 at 12:12 am

    It is correct this model? There is another way to do this?

    There is nothing wrong with this, necessarily. However, events may be a more common approach to handling this, as you’re effectively using the delegate as an event in this scenario.

    Using events does have one significant advantage (potentially), in that you can also easily have multiple subscribers, which would make it simple to allow more than one item to “listen” to the exceptions or log messages. (*While this works with delegates as well, it wouldn’t be as standard of a way to use delegates..)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a class which calculates the distance between two points. I
I have a class that has only one function Print() and two properties age,
I have a scenario where I need to bind two properties from one class
I need to create the ability to drill through an objects properties like two
I need help with the following problem: I have a class with two properties.
I need to create class Dog and PurebredDog extending Dog. Problem is that Dog
I need to create a class to handle audio AudioTrack. In this class must
i need create a variable with parent subclass. Example: Parent Class <?php class parentClass
Why do i need to create an instance of Random class, if i want
Into a class constructor, I need to create some objects on the fly and

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.