Logger.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. namespace SongBrowserPlugin
  3. {
  4. public enum LogLevel
  5. {
  6. Trace,
  7. Debug,
  8. Info,
  9. Warn,
  10. Error
  11. }
  12. public class Logger
  13. {
  14. private readonly string loggerName;
  15. private readonly LogLevel _LogLevel = LogLevel.Info;
  16. private readonly ConsoleColor _defaultFgColor = ConsoleColor.Gray;
  17. public Logger(string _name)
  18. {
  19. loggerName = _name;
  20. }
  21. public void ResetForegroundColor()
  22. {
  23. Console.ForegroundColor = _defaultFgColor;
  24. }
  25. public void Trace(string format, params object[] args)
  26. {
  27. if (_LogLevel > LogLevel.Trace)
  28. {
  29. return;
  30. }
  31. Console.ForegroundColor = ConsoleColor.Cyan;
  32. Console.WriteLine("[" + loggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Trace] " + String.Format(format, args));
  33. ResetForegroundColor();
  34. }
  35. public void Debug(string format, params object[] args)
  36. {
  37. if (_LogLevel > LogLevel.Debug)
  38. {
  39. return;
  40. }
  41. Console.ForegroundColor = ConsoleColor.Magenta;
  42. Console.WriteLine("[" + loggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Debug] " + String.Format(format, args));
  43. ResetForegroundColor();
  44. }
  45. public void Info(string format, params object[] args)
  46. {
  47. if (_LogLevel > LogLevel.Info)
  48. {
  49. return;
  50. }
  51. Console.ForegroundColor = ConsoleColor.Green;
  52. Console.WriteLine("[" + loggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Info] " + String.Format(format, args));
  53. ResetForegroundColor();
  54. }
  55. public 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 void Error(string format, params object[] args)
  66. {
  67. Console.ForegroundColor = ConsoleColor.Yellow;
  68. Console.WriteLine("[" + loggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Error] " + String.Format(format, args));
  69. ResetForegroundColor();
  70. }
  71. public void Exception(string message, Exception e)
  72. {
  73. Console.ForegroundColor = ConsoleColor.Red;
  74. Console.WriteLine("[" + loggerName + " @ " + DateTime.Now.ToString("HH:mm") + "] " + String.Format("{0}-{1}-{2}\n{3}", message, e.GetType().FullName, e.Message, e.StackTrace));
  75. ResetForegroundColor();
  76. }
  77. }
  78. }