You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.7 KiB

  1. #if !NET20
  2. using System;
  3. namespace Apewer.WebSocket
  4. {
  5. /// <summary></summary>
  6. public enum LogLevel
  7. {
  8. /// <summary></summary>
  9. Debug = 0,
  10. /// <summary></summary>
  11. Info = 1,
  12. /// <summary></summary>
  13. Warn = 2,
  14. /// <summary></summary>
  15. Error = 3
  16. }
  17. internal class WebSocketLog
  18. {
  19. public static LogLevel Level = LogLevel.Info;
  20. public static Logger Logger { get; set; }
  21. public static Action<LogLevel, string, Exception> LogAction = (level, message, ex) =>
  22. {
  23. if (level < Level) return;
  24. switch (level)
  25. {
  26. case LogLevel.Debug:
  27. Logger?.Debug(typeof(WebSocketLog), message);
  28. break;
  29. case LogLevel.Info:
  30. Logger?.Text(typeof(WebSocketLog), message);
  31. break;
  32. case LogLevel.Warn:
  33. Logger?.Warning(typeof(WebSocketLog), message);
  34. break;
  35. case LogLevel.Error:
  36. Logger?.Error(typeof(WebSocketLog), message);
  37. break;
  38. }
  39. };
  40. public static void Warn(string message, Exception ex = null)
  41. {
  42. LogAction(LogLevel.Warn, message, ex);
  43. }
  44. public static void Error(string message, Exception ex = null)
  45. {
  46. LogAction(LogLevel.Error, message, ex);
  47. }
  48. public static void Debug(string message, Exception ex = null)
  49. {
  50. LogAction(LogLevel.Debug, message, ex);
  51. }
  52. public static void Info(string message, Exception ex = null)
  53. {
  54. LogAction(LogLevel.Info, message, ex);
  55. }
  56. }
  57. }
  58. #endif