Custom TextBoxListener to write messages into windows form Output

About a year ago I played with Enterprise Library Logging application block and created  TextBoxSink based on the provided DebugTraceSink.  It wasn't fully complete, but now I decided to check can I use it for my current task. 
Because of .Net 2.0 changes  I renamed the class to TextBoxListener, derived it from CustomTraceListener and had to overide TraceData,Write and WriteLine instead of previously overridable SendMessageCore.

It also requred to add in App.config listener in categorySources/listeners and listeners elements.

Now it works with January 2006 version of Enterprise Library, but still not good  enouph for use in general application- at the moment  it requires to set static reference to TextBox. 

 The source code of the  TextBoxListener is the following:

      /// <summary>

            /// Logging sink that writes messages to the Visual Studio      /// <summary>

      /// Logging sink that writes messages to the Visual Studio debugger output.

      /// </summary>

    public class TextBoxListener : CustomTraceListener

      {

            /// <summary>

            /// Property TextBoxToLog (TextBox)

            /// </summary>

            static public TextBox TextBoxToLog

            {

                  get

                  {

                        return st_textBoxToLog;

                  }

                  set

                  {

                        st_textBoxToLog = value;

                  }

            }

            static private TextBox st_textBoxToLog=null;

        //In Sink it was overridable SendMessageCore(LogEntry logEntry)

        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)

        {

            string sToWrite = "";

            if (data is LogEntry && this.Formatter != null)

            {

 

                sToWrite=(this.Formatter.Format(data as LogEntry));

            }

            else

            {

                sToWrite=(data.ToString());

            }

            //this.WriteLine(sToWrite);

            if (null != TextBoxToLog)

            {

                WriteLine(sToWrite);

                //      System.Diagnostics.Debug.WriteLine(SR.TextBoxSinkMessage(logEntry.EventId,logEntry.Message));

            }

        }

 

            private static void AppendToTextBox(string results)

            {

 

            st_textBoxToLog.Text += results;// +Environment.NewLine;

                  st_textBoxToLog.SelectAll();

                  st_textBoxToLog.ScrollToCaret();

            }

        /// <summary>

        /// Writes a message to the debug window

        /// </summary>

        /// <param name="message">The string to write to the debug window</param>

        public override void Write(string message)

        {

            AppendToTextBox(message);

        }

 

        /// <summary>

        /// Writes a message to the debug window

        /// </summary>

        /// <param name="message">The string to write to the debug window</param>

        public override void WriteLine(string message)

        {

            AppendToTextBox(message+ Environment.NewLine);

        }

 

      }

 

The sample testing code is the following:

  

        private void btnLogToTextBox_Click(object sender, EventArgs e)

        {

            //MNF 17/8/2005 modified customizedSinkButton_Click

            try

            {

                Cursor.Current = Cursors.WaitCursor;

                TextBoxListener.TextBoxToLog = this.resultsTextBox;

                for (int i = 0; i < 100; i++)

                {

                    LogEntry log = new LogEntry();

                    log.Message = Properties.Resources.DebugSinkTestMessage + " i= " + i.ToString();

                    log.Priority = 5;

                    log.EventId = 100;

                    log.Categories.Clear();

                    log.Categories.Add("Debug");

                    Logger.Write(log);

                    Logger.Write("My Message", "Debug");

                }

                TextBoxListener.TextBoxToLog = null;

                this.DisplayResults(Properties.Resources.CustomizedSinkEndMessage);

            }

            finally

            {

                Cursor.Current = Cursors.Default;

            }

 

        }

 

The changes in app.config are the following:

<configuration>

      <configSections>

            <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" />

      </configSections>

      <loggingConfiguration tracingEnabled="true" defaultCategory="General">

    <categorySources>

      <add

                        name="Debug"

                        switchValue="All">

        <listeners>

          <add name="Debug Destination" />

          <add name="TextBox Destination" />

        </listeners>

      </add>

    </categorySources>

            <listeners>

      <add name="TextBox Destination"

                        type="LoggingQuickStart.TextBoxListener, LoggingQuickStart"

                        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"

        formatter="Text Formatter"

                        />

    </listeners>

 </loggingConfiguration>
</configuration>

 

 

 

posted @ Monday, August 28, 2006 10:48 AM

Print

Comments on this entry:

# re: Custom TextBoxListener to write messages into windows form Output

Left by shaper at 12/19/2006 9:44 PM
Gravatar
Hello,

Very great !!!!

Working good !

Regards
Sven

# re: Custom TextBoxListener to write messages into windows form Output

Left by Sven at 12/20/2006 12:10 AM
Gravatar
But be aware of Illegal Cross Thread Calls !
I will pass a reference of the UI Form.

# You Rock

Left by Amorano at 4/3/2007 6:31 PM
Gravatar
Denke!

This was great to find. Kudos.

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345