Log.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Y.Utils.LogUtils
  8. {
  9. /// <summary>
  10. /// Log 输出工具
  11. ///
  12. /// 说明:
  13. /// 1、Log.AllocConsole();开启控制台
  14. /// 2、Log.FreeConsole();关闭控制台
  15. /// 3、Log.i("information");输出消息
  16. /// </summary>
  17. public class Log
  18. {
  19. //输出的 Log 格式
  20. const string LogFormat = "{0} {1} {2}";
  21. const string TimeFormat = "MM-dd HH:mm:ss.fff";
  22. #region 输出类型
  23. /// <summary>
  24. /// 输出类型
  25. /// </summary>
  26. enum PrintType
  27. {
  28. v,//verbose 啰嗦的意思
  29. d,//debug 调试的信息
  30. i,//information 一般提示性的消息
  31. w,//warning 警告
  32. e,//error 错误信息
  33. }
  34. #endregion
  35. #region Console 开启/关闭 API
  36. [DllImport("kernel32.dll")]
  37. public static extern Boolean AllocConsole();
  38. [DllImport("kernel32.dll")]
  39. public static extern Boolean FreeConsole();
  40. #endregion
  41. #region 输出颜色
  42. /// <summary>
  43. /// 获取输出颜色
  44. /// </summary>
  45. /// <param name="type">输出类型</param>
  46. /// <returns></returns>
  47. private static ConsoleColor GetColor(PrintType type)
  48. {
  49. switch (type)
  50. {
  51. case PrintType.v: return ConsoleColor.Gray;
  52. case PrintType.d: return ConsoleColor.Blue;
  53. case PrintType.i: return ConsoleColor.Green;
  54. case PrintType.w: return ConsoleColor.Yellow;
  55. case PrintType.e: return ConsoleColor.Red;
  56. default: return ConsoleColor.Gray;
  57. }
  58. }
  59. #endregion
  60. #region 写出 Log
  61. /// <summary>
  62. /// 写出到控制台
  63. /// </summary>
  64. /// <param name="type">类型</param>
  65. /// <param name="tag">标记</param>
  66. /// <param name="message">消息</param>
  67. private static void Write(PrintType type, string message)
  68. {
  69. DateTime now = DateTime.Now;
  70. Console.ForegroundColor = GetColor(type);
  71. Console.WriteLine(LogFormat, now.ToString(TimeFormat), type.ToString(), message);
  72. }
  73. #endregion
  74. #region 分类详细输出
  75. /// <summary>
  76. /// 输出 verbose (啰嗦信息)
  77. /// </summary>
  78. /// <param name="message">消息</param>
  79. /// <param name="tag">可选:标记</param>
  80. public static void v(string message)
  81. {
  82. Write(PrintType.v, message);
  83. }
  84. /// <summary>
  85. /// 输出 Debug (调试信息)
  86. /// </summary>
  87. /// <param name="message">消息</param>
  88. /// <param name="tag">可选:标记</param>
  89. public static void d(string message)
  90. {
  91. Write(PrintType.d, message);
  92. }
  93. /// <summary>
  94. /// 输出 Information (重要信息)
  95. /// </summary>
  96. /// <param name="message">消息</param>
  97. /// <param name="tag">可选:标记</param>
  98. public static void i(string message)
  99. {
  100. Write(PrintType.i, message);
  101. }
  102. /// <summary>
  103. /// 输出 Warning (警告信息)
  104. /// </summary>
  105. /// <param name="message">消息</param>
  106. /// <param name="tag">可选:标记</param>
  107. public static void w(string message)
  108. {
  109. Write(PrintType.w, message);
  110. }
  111. /// <summary>
  112. /// 输出 Error (错误信息)
  113. /// </summary>
  114. /// <param name="message">消息</param>
  115. /// <param name="tag">可选:标记</param>
  116. public static void e(string message)
  117. {
  118. Write(PrintType.e, message);
  119. }
  120. #endregion
  121. }
  122. }