Logger.cs 2.8 KB

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