Logger.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Diagnostics;
  3. namespace KYFramework
  4. {
  5. public class Logger
  6. {
  7. private static Logger instance;
  8. public static Logger Instance
  9. {
  10. get
  11. {
  12. if (instance == null) instance = new Logger();
  13. return instance;
  14. }
  15. }
  16. private ILog iLog;
  17. public ILog ILog
  18. {
  19. get
  20. {
  21. return iLog;
  22. }
  23. set
  24. {
  25. this.iLog = value;
  26. }
  27. }
  28. private const int TraceLevel = 1;
  29. private const int DebugLevel = 2;
  30. private const int InfoLevel = 3;
  31. private const int WarningLevel = 4;
  32. private bool CheckLogLevel(int level)
  33. {
  34. if (OptionComponent.Instance == null)
  35. {
  36. return true;
  37. }
  38. return OptionComponent.Instance.option.LogLevel <= level;
  39. }
  40. public void Trace(string msg)
  41. {
  42. if (!CheckLogLevel(DebugLevel))
  43. {
  44. return;
  45. }
  46. StackTrace st = new StackTrace(2, true);
  47. Logger.Instance.Trace($"{msg}\n{st}");
  48. }
  49. public void Debug(string msg)
  50. {
  51. if (!CheckLogLevel(DebugLevel))
  52. {
  53. return;
  54. }
  55. this.iLog?.Debug(msg);
  56. }
  57. public void Info(string msg)
  58. {
  59. if (!CheckLogLevel(InfoLevel))
  60. {
  61. return;
  62. }
  63. this.iLog?.Info(msg);
  64. }
  65. public void TraceInfo(string msg)
  66. {
  67. if (!CheckLogLevel(InfoLevel))
  68. {
  69. return;
  70. }
  71. StackTrace st = new StackTrace(2, true);
  72. this.iLog?.Trace($"{msg}\n{st}");
  73. }
  74. public void Warning(string msg)
  75. {
  76. if (!CheckLogLevel(WarningLevel))
  77. {
  78. return;
  79. }
  80. this.iLog?.Warning(msg);
  81. }
  82. public void Error(string msg)
  83. {
  84. StackTrace st = new StackTrace(2, true);
  85. this.iLog?.Error($"{msg}\n{st}");
  86. }
  87. public void Error(Exception e)
  88. {
  89. if (e.Data.Contains("StackTrace"))
  90. {
  91. this.iLog?.Error($"{e.Data["StackTrace"]}\n{e}");
  92. return;
  93. }
  94. string str = e.ToString();
  95. this.iLog?.Error(str);
  96. }
  97. public void Trace(string message, params object[] args)
  98. {
  99. if (!CheckLogLevel(TraceLevel))
  100. {
  101. return;
  102. }
  103. StackTrace st = new StackTrace(2, true);
  104. this.iLog?.Trace($"{string.Format(message, args)}\n{st}");
  105. }
  106. public void Warning(string message, params object[] args)
  107. {
  108. if (!CheckLogLevel(WarningLevel))
  109. {
  110. return;
  111. }
  112. this.iLog?.Warning(string.Format(message, args));
  113. }
  114. public void Info(string message, params object[] args)
  115. {
  116. if (!CheckLogLevel(InfoLevel))
  117. {
  118. return;
  119. }
  120. this.iLog?.Info(string.Format(message, args));
  121. }
  122. public void Debug(string message, params object[] args)
  123. {
  124. if (!CheckLogLevel(DebugLevel))
  125. {
  126. return;
  127. }
  128. this.iLog?.Debug(string.Format(message, args));
  129. }
  130. public void Error(string message, params object[] args)
  131. {
  132. StackTrace st = new StackTrace(2, true);
  133. string s = string.Format(message, args) + '\n' + st;
  134. this.iLog?.Error(s);
  135. }
  136. }
  137. }