Colored console writes C#


/ Published in: C#
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. public static class DebugConsole {
  2. private static bool Writing = false;
  3.  
  4. /// <summary>
  5. /// Write an object's string representation to the console
  6. /// </summary>
  7. /// <param name="color">Color to write in</param>
  8. /// <param name="src">Originating object</param>
  9. /// <param name="format">String format</param>
  10. /// <param name="args">String format args</param>
  11. public static void Write(ConsoleColor color, object src, string format, params object[] args) {
  12. _write(color, src, String.Format(format, args));
  13. }
  14. /// <summary>
  15. /// Write an object's string representation to the console with a
  16.  
  17. /// </summary>
  18. /// <param name="color">Color to write in</param>
  19. /// <param name="src">Originating object</param>
  20. /// <param name="format">String format</param>
  21. /// <param name="args">String format args</param>
  22. public static void WriteLine(ConsoleColor color, object src, string format, params object[] args) {
  23. _write(color, src, String.Format(format + "
  24. ", args));
  25. }
  26.  
  27. /// <summary>
  28. /// Write an object's string representation to the console
  29. /// </summary>
  30. /// <param name="color">Color to write in</param>
  31. /// <param name="src">Originating object</param>
  32. /// <param name="data">Object to write</param>
  33. public static void Write(ConsoleColor color, object src, object data) {
  34. _write(color, src, data.ToString());
  35. }
  36. /// <summary>
  37. /// Write an object's string representation to the console with a
  38.  
  39. /// </summary>
  40. /// <param name="color">Color to write in</param>
  41. /// <param name="src">Originating object</param>
  42. /// <param name="data">Object to write</param>
  43. public static void WriteLine(ConsoleColor color, object src, object data) {
  44. _write(color, src, data.ToString() + "
  45. ");
  46. }
  47.  
  48. private static void _write(ConsoleColor color, object src, string data) {
  49. while (Writing) {
  50. //Wait for the previous write operation to finish before beginning this one
  51.  
  52. //Wait for 10 milliseconds to allow the console buffer to flush
  53. //once the previous write opperation finishes
  54. System.Threading.Thread.Sleep(10);
  55. }
  56.  
  57. //Stop any other write operations from writing
  58. Writing = true;
  59.  
  60. //Set restore color
  61. ConsoleColor bak = Console.ForegroundColor;
  62.  
  63. //Write src name in the passed color
  64. Console.ForegroundColor = color;
  65. Console.Write(src);
  66.  
  67. //Write the originating thread id in white
  68. Console.ForegroundColor = ConsoleColor.White;
  69. Console.Write("[{0}]", System.Threading.Thread.CurrentThread.ManagedThreadId);
  70.  
  71. //Write the divider in yellow
  72. Console.ForegroundColor = ConsoleColor.Yellow;
  73. Console.Write("::");
  74.  
  75. //Write the data in the passed color
  76. Console.ForegroundColor = color;
  77. Console.Write(data);
  78.  
  79. //Restore the color
  80. Console.ForegroundColor = bak;
  81.  
  82. //Allow next write opperation to continue
  83. Writing = false;
  84. }
  85. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.