4

2 つのプロパティを持つクラスを作成する必要があります。

  1. ログ出力
  2. 例外出力

これらのプロパティ (Actions<>) は、ターゲット関数に応じてメッセージまたは例外を送信します。このターゲット関数は、プロパティを介して設定されます。

現在、私はこの機能コードを持っています:

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

そして、これは私のフォームコードです:

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

このモデルで正しいですか?これを行う別の方法はありますか?

4

2 に答える 2

8

このモデルで正しいですか?これを行う別の方法はありますか?

必然的に、これには何も問題はありません。ただし、このシナリオではデリゲートをイベントとして効果的に使用しているため、イベント はこれを処理するためのより一般的なアプローチである可能性があります。

イベントを使用すると、複数のサブスクライバーを簡単に持つことができるという点で (潜在的に) 1 つの大きな利点があります。これにより、複数のアイテムが例外やログ メッセージを "リッスン" することが簡単になります。(*これはデリゲートでも機能しますが、デリゲートを使用する標準的な方法ではありません..)

于 2012-07-05T15:43:31.037 に答える
1

トピック外で申し訳ありませんがStringBuilder string、編集には使用しないでください

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


            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format("Program:{0}", message));
            sb.Append(string.Format("Message{0}", ex.Message));
            sb.Append(string.Format("StackTrace:{0}", ex.StackTrace));
            sb.Append(string.Format("Source:{0}", ex.Source));
            string result = sb.ToString();
于 2012-07-05T15:45:55.397 に答える