/ Published in: C#
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.
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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; } }