Logger.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.Info;
  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. Plugin.Log.Debug(String.Format(format, args));
  34. }
  35. public static void Info(string format, params object[] args)
  36. {
  37. Plugin.Log.Info(String.Format(format, args));
  38. }
  39. public static void Log(string format, params object[] args)
  40. {
  41. Logger.Debug(format, args);
  42. }
  43. public static void Warning(string format, params object[] args)
  44. {
  45. Plugin.Log.Warn(String.Format(format, args));
  46. }
  47. public static void Error(Exception e)
  48. {
  49. Logger.Error("{0}", e.Message);
  50. }
  51. public static void Error(string format, params object[] args)
  52. {
  53. Plugin.Log.Error(String.Format(format, args));
  54. }
  55. public static void Exception(string message)
  56. {
  57. Plugin.Log.Critical(message);
  58. }
  59. public static void Exception(Exception e)
  60. {
  61. Plugin.Log.Critical(e);
  62. }
  63. public static void Exception(string message, Exception e)
  64. {
  65. Plugin.Log.Warn($"{message}:{e}");
  66. }
  67. }
  68. }