Logger.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. namespace SongBrowser.Logging
  3. {
  4. public enum LogLevel
  5. {
  6. Trace,
  7. Debug,
  8. Info,
  9. Warn,
  10. Error
  11. }
  12. public class Logger
  13. {
  14. private static readonly string LoggerName = "SongBrowser";
  15. private static readonly LogLevel LogLevel = LogLevel.Debug;
  16. private static readonly ConsoleColor DefaultFgColor = ConsoleColor.Gray;
  17. private static void ResetForegroundColor()
  18. {
  19. Console.ForegroundColor = DefaultFgColor;
  20. }
  21. public static void Trace(string format, params object[] args)
  22. {
  23. if (LogLevel > LogLevel.Trace)
  24. {
  25. return;
  26. }
  27. Console.ForegroundColor = ConsoleColor.Cyan;
  28. Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Trace] " + String.Format(format, args));
  29. ResetForegroundColor();
  30. }
  31. public static void Debug(string format, params object[] args)
  32. {
  33. if (LogLevel > LogLevel.Debug)
  34. {
  35. return;
  36. }
  37. Console.ForegroundColor = ConsoleColor.Magenta;
  38. Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Debug] " + String.Format(format, args));
  39. ResetForegroundColor();
  40. }
  41. public static void Info(string format, params object[] args)
  42. {
  43. if (LogLevel > LogLevel.Info)
  44. {
  45. return;
  46. }
  47. Console.ForegroundColor = ConsoleColor.Green;
  48. Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Info] " + String.Format(format, args));
  49. ResetForegroundColor();
  50. }
  51. public static void Log(string format, params object[] args)
  52. {
  53. Logger.Debug(format, args);
  54. }
  55. public static void Warning(string format, params object[] args)
  56. {
  57. if (LogLevel > LogLevel.Warn)
  58. {
  59. return;
  60. }
  61. Console.ForegroundColor = ConsoleColor.Blue;
  62. Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Warning] " + String.Format(format, args));
  63. ResetForegroundColor();
  64. }
  65. public static void Error(Exception e)
  66. {
  67. Logger.Error("{0}", e.Message);
  68. }
  69. public static void Error(string format, params object[] args)
  70. {
  71. Console.ForegroundColor = ConsoleColor.Yellow;
  72. Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Error] " + String.Format(format, args));
  73. ResetForegroundColor();
  74. }
  75. public static void Exception(string message)
  76. {
  77. Console.ForegroundColor = ConsoleColor.Red;
  78. Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + "] " + String.Format("{0}", message));
  79. ResetForegroundColor();
  80. }
  81. public static void Exception(Exception e)
  82. {
  83. Console.ForegroundColor = ConsoleColor.Red;
  84. Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + "] " + String.Format("{0}", e));
  85. ResetForegroundColor();
  86. }
  87. public static void Exception(string message, Exception e)
  88. {
  89. Console.ForegroundColor = ConsoleColor.Red;
  90. Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + "] " + String.Format("{0}-{1}-{2}\n{3}", message, e.GetType().FullName, e.Message, e.StackTrace));
  91. ResetForegroundColor();
  92. }
  93. }
  94. }