Log.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. WriteFile(type, message);
  70. }
  71. private static void WriteFile(PrintType type, string message)
  72. {
  73. lock (LogFileLock)
  74. {
  75. //设置日志目录
  76. string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log";
  77. string file = string.Format(@"{0}\{1}.txt", logPath, DateTime.Now.ToString("yyyy-MM-dd"));
  78. //创建日志目录
  79. DirTool.Create(logPath);
  80. //写出日志
  81. TxtTool.Append(file, string.Format(LogFormat, DateTime.Now.ToString(TimeFormat), type.ToString(), message));
  82. }
  83. }
  84. #region 分类详细输出
  85. /// <summary>
  86. /// 输出 verbose (啰嗦信息)
  87. /// </summary>
  88. /// <param name="message">消息</param>
  89. /// <param name="tag">可选:标记</param>
  90. public static void v<T>(T msg)
  91. {
  92. Write(PrintType.v, msg.ToString());
  93. }
  94. /// <summary>
  95. /// 输出 Debug (调试信息)
  96. /// </summary>
  97. /// <param name="message">消息</param>
  98. /// <param name="tag">可选:标记</param>
  99. public static void d<T>(T msg)
  100. {
  101. Write(PrintType.d, msg.ToString());
  102. }
  103. /// <summary>
  104. /// 输出 Information (重要信息)
  105. /// </summary>
  106. /// <param name="message">消息</param>
  107. /// <param name="tag">可选:标记</param>
  108. public static void i<T>(T msg)
  109. {
  110. Write(PrintType.i, msg.ToString());
  111. }
  112. /// <summary>
  113. /// 输出 Warning (警告信息)
  114. /// </summary>
  115. /// <param name="message">消息</param>
  116. /// <param name="tag">可选:标记</param>
  117. public static void w<T>(T msg)
  118. {
  119. Write(PrintType.w, msg.ToString());
  120. }
  121. /// <summary>
  122. /// 输出 Error (错误信息)
  123. /// </summary>
  124. /// <param name="message">消息</param>
  125. /// <param name="tag">可选:标记</param>
  126. public static void e<T>(T msg)
  127. {
  128. Write(PrintType.e, msg.ToString());
  129. }
  130. #endregion
  131. }
  132. }