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.

235 lines
7.2 KiB

  1. using Apewer.Internals;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Threading;
  6. namespace Apewer
  7. {
  8. /// <summary>日志记录程序。</summary>
  9. public class Logger
  10. {
  11. private string _key = Guid.NewGuid().ToString("n");
  12. private string _target = "";
  13. private bool _enabled = true;
  14. /// <summary>异常。</summary>
  15. public event Event<Exception> ExceptionEvent;
  16. /// <summary>调试。</summary>
  17. public event Event<string> DebugEvent;
  18. /// <summary>文本。</summary>
  19. public event Event<string> TextEvent;
  20. /// <summary>信息。</summary>
  21. public event Event<string> InfomationEvent;
  22. /// <summary>注意。</summary>
  23. public event Event<string> WarningEvent;
  24. /// <summary>错误。</summary>
  25. public event Event<string> ErrorEvent;
  26. /// <summary>自定义。</summary>
  27. public event Event<object> CustomEvent;
  28. /// <summary>已启用。</summary>
  29. public bool Enabled { get { return _enabled; } set { _enabled = value; } }
  30. /// <summary>唯一标识。</summary>
  31. public string Key { get { return _key; } }
  32. /// <summary>目标。</summary>
  33. public string Target { get { return _target; } protected set { _target = value ?? ""; } }
  34. internal void Invoke(LogItem item)
  35. {
  36. if (item == null) return;
  37. switch (item.Type)
  38. {
  39. case LogType.Debug:
  40. if (DebugEvent != null) DebugEvent(this, item.Content);
  41. break;
  42. case LogType.Text:
  43. if (TextEvent != null) TextEvent(this, item.Content);
  44. break;
  45. case LogType.Infomation:
  46. if (InfomationEvent != null) InfomationEvent(this, item.Content);
  47. break;
  48. case LogType.Warning:
  49. if (WarningEvent != null) WarningEvent(this, item.Content);
  50. break;
  51. case LogType.Error:
  52. if (ErrorEvent != null) ErrorEvent(this, item.Content);
  53. break;
  54. case LogType.Exception:
  55. if (ExceptionEvent != null) ExceptionEvent(this, item.Exception);
  56. break;
  57. case LogType.Custom:
  58. if (CustomEvent != null) CustomEvent(this, item.Custom);
  59. break;
  60. }
  61. }
  62. /// <summary>异常。</summary>
  63. public void Exception(Exception value)
  64. {
  65. if (value == null) return;
  66. var item = new LogItem(LogType.Exception);
  67. item.Logger = this;
  68. item.Target = _target;
  69. try { item.Content = value.ToString(); } catch { }
  70. }
  71. /// <summary>自定义。</summary>
  72. public void Custom(object value)
  73. {
  74. if (value == null) return;
  75. var item = new LogItem(LogType.Custom);
  76. item.Logger = this;
  77. item.Target = _target;
  78. item.Custom = value;
  79. }
  80. /// <summary>调试。</summary>
  81. public void Debug(string value)
  82. {
  83. if (value == null) return;
  84. var item = new LogItem(LogType.Debug);
  85. item.Logger = this;
  86. item.Target = _target;
  87. item.Content = value;
  88. }
  89. /// <summary>文本。</summary>
  90. public void Text(string value)
  91. {
  92. if (value == null) return;
  93. var item = new LogItem(LogType.Text);
  94. item.Logger = this;
  95. item.Target = _target;
  96. item.Content = value;
  97. }
  98. /// <summary>信息。</summary>
  99. public void Infomation(string value)
  100. {
  101. if (value == null) return;
  102. var item = new LogItem(LogType.Infomation);
  103. item.Logger = this;
  104. item.Target = _target;
  105. item.Content = value;
  106. }
  107. /// <summary>注意。</summary>
  108. public void Warning(string value)
  109. {
  110. if (value == null) return;
  111. var item = new LogItem(LogType.Warning);
  112. item.Logger = this;
  113. item.Target = _target;
  114. item.Content = value;
  115. }
  116. /// <summary>错误。</summary>
  117. public void Error(string value)
  118. {
  119. if (value == null) return;
  120. var item = new LogItem(LogType.Error);
  121. item.Logger = this;
  122. item.Target = _target;
  123. item.Content = value;
  124. }
  125. private Logger(string target)
  126. {
  127. _target = target ?? "";
  128. }
  129. private static Event<Exception> ExceptionDefaultCallback = null;
  130. private static Event<object> CustomDefaultCallback = null;
  131. private static Event<string> DebugDefaultCallback = null;
  132. private static Event<string> TextDefaultCallback = null;
  133. private static Event<string> InfomationDefaultCallback = null;
  134. private static Event<string> WarningDefaultCallback = null;
  135. private static Event<string> ErrorDefaultCallback = null;
  136. private static void Logger_ExceptionEvent(object sender, Exception value)
  137. {
  138. if (ExceptionDefaultCallback != null) ExceptionDefaultCallback(sender, value);
  139. }
  140. private static void Logger_CustomEvent(object sender, object value)
  141. {
  142. if (ExceptionDefaultCallback != null) CustomDefaultCallback(sender, value);
  143. }
  144. private static void Logger_DebugEvent(object sender, string value)
  145. {
  146. if (ExceptionDefaultCallback != null) DebugDefaultCallback(sender, value);
  147. }
  148. private static void Logger_TextEvent(object sender, string value)
  149. {
  150. throw new NotImplementedException();
  151. }
  152. private static void Logger_InfomationEvent(object sender, string value)
  153. {
  154. throw new NotImplementedException();
  155. }
  156. private static void Logger_WarningEvent(object sender, string value)
  157. {
  158. throw new NotImplementedException();
  159. }
  160. private static void Logger_ErrorEvent(object sender, string value)
  161. {
  162. }
  163. private Logger(Type target)
  164. {
  165. try
  166. {
  167. if (target != null)
  168. {
  169. _target = target.FullName;
  170. }
  171. }
  172. catch { }
  173. }
  174. private Logger(object target)
  175. {
  176. try
  177. {
  178. if (target != null)
  179. {
  180. _target = target.GetType().FullName;
  181. }
  182. }
  183. catch { }
  184. }
  185. /// <summary>默认的日志记录程序。</summary>
  186. public static Logger Default(object target)
  187. {
  188. var logger = new Logger(target);
  189. logger.CustomEvent += Logger_CustomEvent;
  190. logger.ExceptionEvent += Logger_ExceptionEvent;
  191. logger.DebugEvent += Logger_DebugEvent;
  192. logger.TextEvent += Logger_TextEvent;
  193. logger.InfomationEvent += Logger_InfomationEvent;
  194. logger.WarningEvent += Logger_WarningEvent;
  195. logger.ErrorEvent += Logger_ErrorEvent;
  196. return logger;
  197. }
  198. }
  199. }