QLog.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Threading;
  6. using System.Diagnostics;
  7. namespace COSXML.Log
  8. {
  9. public sealed class QLog
  10. {
  11. private static string timeFormat = "yyyy-MM-dd HH:mm:ss.fff";
  12. //private static string currentDir = Directory.GetCurrentDirectory();
  13. private static Process currentProcess = Process.GetCurrentProcess();
  14. private static List<ILog> logImplList = new List<ILog>();
  15. private static Level level = Level.V;
  16. public static void SetLogLevel(Level level)
  17. {
  18. QLog.level = level;
  19. }
  20. public static void AddLogAdapter(ILog log)
  21. {
  22. if (log == null)
  23. {
  24. return;
  25. }
  26. foreach (ILog logImpl in logImplList)
  27. {
  28. if (logImpl.GetType().Name == log.GetType().Name)
  29. {
  30. return;
  31. }
  32. }
  33. logImplList.Add(log);
  34. }
  35. public static void Verbose(string tag, string message)
  36. {
  37. Verbose(tag, message, null);
  38. }
  39. public static void Verbose(string tag, string message, Exception exception)
  40. {
  41. if (Level.V >= QLog.level)
  42. {
  43. Print(Level.V, tag, message, exception);
  44. }
  45. }
  46. public static void Debug(string tag, string message)
  47. {
  48. Debug(tag, message, null);
  49. }
  50. public static void Debug(string tag, string message, Exception exception)
  51. {
  52. if (Level.D >= QLog.level)
  53. {
  54. Print(Level.D, tag, message, exception);
  55. }
  56. }
  57. public static void Info(string tag, string message)
  58. {
  59. Info(tag, message, null);
  60. }
  61. public static void Info(string tag, string message, Exception exception)
  62. {
  63. if (Level.I >= QLog.level)
  64. {
  65. Print(Level.I, tag, message, exception);
  66. }
  67. }
  68. public static void Warn(string tag, string message)
  69. {
  70. Warn(tag, message, null);
  71. }
  72. public static void Warn(string tag, string message, Exception exception)
  73. {
  74. if (Level.W >= QLog.level)
  75. {
  76. Print(Level.W, tag, message, exception);
  77. }
  78. }
  79. public static void Error(string tag, string message)
  80. {
  81. Error(tag, message, null);
  82. }
  83. public static void Error(string tag, string message, Exception exception)
  84. {
  85. if (Level.E >= QLog.level)
  86. {
  87. Print(Level.E, tag, message, exception);
  88. }
  89. }
  90. private static void Print(Level level, string tag, string message, Exception exception)
  91. {
  92. StringBuilder messageBuilder = new StringBuilder();
  93. messageBuilder.Append(DateTime.Now.ToString(timeFormat))
  94. .Append(" ")
  95. .Append(Thread.CurrentThread.ManagedThreadId)
  96. .Append("-")
  97. .Append(currentProcess.Id)
  98. // OSX 系统上不支持
  99. // .Append("/")
  100. // .Append(currentProcess.ProcessName)
  101. .Append(" ")
  102. .Append(level.ToString())
  103. .Append("/")
  104. .Append(tag)
  105. .Append(": ")
  106. .Append(message);
  107. if (exception != null)
  108. {
  109. messageBuilder.Append("\n Exception:\n")
  110. .Append(exception.ToString());
  111. }
  112. messageBuilder.Append("\r\n");
  113. foreach (ILog log in logImplList)
  114. {
  115. log.PrintLog(messageBuilder.ToString());
  116. }
  117. }
  118. //private static string PrintExceptionTrace(Exception exception)
  119. //{
  120. // return exception.ToString();
  121. //}
  122. }
  123. public enum Level
  124. {
  125. V = 0,
  126. D,
  127. I,
  128. W,
  129. E
  130. }
  131. }