Return to Snippet

Revision: 24862
at April 9, 2017 03:02 by Bajotumn


Updated Code
public static class DebugConsole {
        private static bool Writing = false;

        /// <summary>
        /// Write an object's string representation to the console
        /// </summary>
        /// <param name="color">Color to write in</param>
        /// <param name="src">Originating object</param>
        /// <param name="format">String format</param>
        /// <param name="args">String format args</param>
        public static void Write(ConsoleColor color, object src, string format, params object[] args) {
            _write(color, src, String.Format(format, args));
        }
        /// <summary>
        /// Write an object's string representation to the console with a 

        /// </summary>
        /// <param name="color">Color to write in</param>
        /// <param name="src">Originating object</param>
        /// <param name="format">String format</param>
        /// <param name="args">String format args</param>
        public static void WriteLine(ConsoleColor color, object src, string format, params object[] args) {
            _write(color, src, String.Format(format + "
", args));
        }

        /// <summary>
        /// Write an object's string representation to the console
        /// </summary>
        /// <param name="color">Color to write in</param>
        /// <param name="src">Originating object</param>
        /// <param name="data">Object to write</param>
        public static void Write(ConsoleColor color, object src, object data) {
            _write(color, src, data.ToString());
        }
        /// <summary>
        /// Write an object's string representation to the console with a 

        /// </summary>
        /// <param name="color">Color to write in</param>
        /// <param name="src">Originating object</param>
        /// <param name="data">Object to write</param>
        public static void WriteLine(ConsoleColor color, object src, object data) {
            _write(color, src, data.ToString() + "
");
        }

        private static void _write(ConsoleColor color, object src, string data) {
            while (Writing) {
                //Wait for the previous write operation to finish before beginning this one

                //Wait for 10 milliseconds to allow the console buffer to flush
                //once the previous write opperation finishes
                System.Threading.Thread.Sleep(10);
            }

            //Stop any other write operations from writing
            Writing = true;

            //Set restore color
            ConsoleColor bak = Console.ForegroundColor;

            //Write src name in the passed color
            Console.ForegroundColor = color;
            Console.Write(src);

            //Write the originating thread id in white
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("[{0}]", System.Threading.Thread.CurrentThread.ManagedThreadId);

            //Write the divider in yellow
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("::");

            //Write the data in the passed color
            Console.ForegroundColor = color;
            Console.Write(data);

            //Restore the color
            Console.ForegroundColor = bak;

            //Allow next write opperation to continue
            Writing = false;
        }
    }

Revision: 24861
at March 12, 2010 15:35 by Bajotumn


Initial Code
public static class DebugConsole {
        private static bool Writing = false;

        /// <summary>
        /// Write an object's string representation to the console
        /// </summary>
        /// <param name="color">Color to write in</param>
        /// <param name="src">Originating object</param>
        /// <param name="format">String format</param>
        /// <param name="args">String format args</param>
        public static void Write(ConsoleColor color, object src, string format, params object[] args) {
            _write(color, src, String.Format(format, args));
        }
        /// <summary>
        /// Write an object's string representation to the console with a 

        /// </summary>
        /// <param name="color">Color to write in</param>
        /// <param name="src">Originating object</param>
        /// <param name="format">String format</param>
        /// <param name="args">String format args</param>
        public static void WriteLine(ConsoleColor color, object src, string format, params object[] args) {
            _write(color, src, String.Format(format + "
", args));
        }

        /// <summary>
        /// Write an object's string representation to the console
        /// </summary>
        /// <param name="color">Color to write in</param>
        /// <param name="src">Originating object</param>
        /// <param name="data">Object to write</param>
        public static void Write(ConsoleColor color, object src, object data) {
            _write(color, src, data.ToString());
        }
        /// <summary>
        /// Write an object's string representation to the console with a 

        /// </summary>
        /// <param name="color">Color to write in</param>
        /// <param name="src">Originating object</param>
        /// <param name="data">Object to write</param>
        public static void WriteLine(ConsoleColor color, object src, object data) {
            _write(color, src, data.ToString() + "
");
        }

        private static void _write(ConsoleColor color, object src, string data) {
            while (Writing) {
                //Wait for the previous write operation to finish before beginning this one

                //Wait for 10 milliseconds to allow the console buffer to flush
                //once the previous write opperation finishes
                System.Threading.Thread.Sleep(10);
            }

            //Stop any other write operations from writing
            Writing = true;

            //Set restore color
            ConsoleColor bak = Console.ForegroundColor;

            //Write src name in the passed color
            Console.ForegroundColor = color;
            Console.Write(src);

            //Write the originating thread id in white
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("[{0}]", System.Threading.Thread.CurrentThread.ManagedThreadId);

            //Write the divider in yellow
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("::");

            //Write the data in the passed color
            Console.ForegroundColor = color;
            Console.Write(data);

            //Restore the color
            Console.ForegroundColor = bak;

            //Allow next write opperation to continue
            Writing = false;
        }
    }

Initial URL


Initial Description
Writes text to the console in multiple colors to allow easy distinguishing between different write sources.

I use this heavily on multithreaded apps because it performs write operations synchronously, so that each write can not overlap another when writing in colors and multiple parts.

Initial Title
Colored console writes C#

Initial Tags


Initial Language
C#