Logger.cs 3.9 KB

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