Log.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using Y.Utils.Net20.FileUtils;
  6. using Y.Utils.Net20.TxtUtils;
  7. namespace Y.Utils.Net20.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. private static object LogFileLock = new object();
  23. #region 输出类型
  24. /// <summary>
  25. /// 输出类型
  26. /// </summary>
  27. enum PrintType
  28. {
  29. v,//verbose 啰嗦的意思
  30. d,//debug 调试的信息
  31. i,//information 一般提示性的消息
  32. w,//warning 警告
  33. e,//error 错误信息
  34. }
  35. #endregion
  36. #region Console 开启/关闭 API
  37. [DllImport("kernel32.dll")]
  38. public static extern Boolean AllocConsole();
  39. [DllImport("kernel32.dll")]
  40. public static extern Boolean FreeConsole();
  41. #endregion
  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. /// <summary>
  60. /// 写出到控制台
  61. /// </summary>
  62. /// <param name="type">类型</param>
  63. /// <param name="tag">标记</param>
  64. /// <param name="message">消息</param>
  65. private static void Write(PrintType type, string message)
  66. {
  67. Console.ForegroundColor = GetColor(type);
  68. Console.WriteLine(LogFormat, DateTime.Now.ToString(TimeFormat), type.ToString(), message);
  69. }
  70. private static void WriteFile(PrintType type, string message)
  71. {
  72. lock (LogFileLock)
  73. {
  74. //设置日志目录
  75. string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log";
  76. string file = string.Format(@"{0}\{1}.txt", logPath, DateTime.Now.ToString("yyyy-MM-dd"));
  77. //创建日志目录
  78. DirTool.Create(logPath);
  79. //写出日志
  80. TxtTool.Append(file, string.Format(LogFormat, DateTime.Now.ToString(TimeFormat), type.ToString(), message));
  81. }
  82. }
  83. #region 分类详细输出
  84. /// <summary>
  85. /// 输出 verbose (啰嗦信息)
  86. /// </summary>
  87. /// <param name="message">消息</param>
  88. /// <param name="tag">可选:标记</param>
  89. public static void v<T>(T msg)
  90. {
  91. Write(PrintType.v, msg.ToString());
  92. }
  93. /// <summary>
  94. /// 输出 Debug (调试信息)
  95. /// </summary>
  96. /// <param name="message">消息</param>
  97. /// <param name="tag">可选:标记</param>
  98. public static void d<T>(T msg)
  99. {
  100. Write(PrintType.d, msg.ToString());
  101. }
  102. /// <summary>
  103. /// 输出 Information (重要信息)
  104. /// </summary>
  105. /// <param name="message">消息</param>
  106. /// <param name="tag">可选:标记</param>
  107. public static void i<T>(T msg)
  108. {
  109. Write(PrintType.i, msg.ToString());
  110. }
  111. /// <summary>
  112. /// 输出 Warning (警告信息)
  113. /// </summary>
  114. /// <param name="message">消息</param>
  115. /// <param name="tag">可选:标记</param>
  116. public static void w<T>(T msg)
  117. {
  118. Write(PrintType.w, msg.ToString());
  119. }
  120. /// <summary>
  121. /// 输出 Error (错误信息)
  122. /// </summary>
  123. /// <param name="message">消息</param>
  124. /// <param name="tag">可选:标记</param>
  125. public static void e<T>(T msg)
  126. {
  127. Write(PrintType.e, msg.ToString());
  128. }
  129. #endregion
  130. }
  131. }