Log.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //############################################################
  2. // https://github.com/yuzhengyang
  3. // author:yuzhengyang
  4. //############################################################
  5. //R.Log.IsWriteFile = true;
  6. //R.Log.LogLevel = LogLevel.Warning | LogLevel.Debug;
  7. //Log.AllocConsole();
  8. //R.Log.v("this is v 啰嗦");
  9. //R.Log.d("this is d 调试");
  10. //R.Log.i("this is i 重要");
  11. //R.Log.w("this is w 警告");
  12. //R.Log.e("this is e 错误");
  13. using System;
  14. using System.Runtime.InteropServices;
  15. using Y.Utils.IOUtils.PathUtils;
  16. using Y.Utils.IOUtils.TxtUtils;
  17. namespace Y.Utils.IOUtils.LogUtils
  18. {
  19. /// <summary>
  20. /// Log 输出工具
  21. ///
  22. /// 说明:
  23. /// 1、Log.AllocConsole();开启控制台
  24. /// 2、Log.FreeConsole();关闭控制台
  25. /// 3、Log.i("information");输出消息
  26. /// </summary>
  27. public class Log
  28. {
  29. //输出的 Log 格式
  30. const string LogFormat = "{0} {1} {2}";
  31. const string TimeFormat = "HH:mm:ss.fff";
  32. private object LogFileLock = new object();//写日志文件锁
  33. private bool IsWriteFile = false;//是否写日志文件
  34. public string LogPath = "Log";
  35. public LogLevel LogLevel = LogLevel.All;//日志输出等级
  36. public bool SetWriteFile(bool isWrite, string logPath)
  37. {
  38. if (isWrite && !string.IsNullOrWhiteSpace(logPath))
  39. {
  40. LogPath = logPath.Trim();
  41. IsWriteFile = true;
  42. return true;
  43. }
  44. IsWriteFile = false;
  45. return false;
  46. }
  47. #region Console 开启/关闭 API
  48. [DllImport("kernel32.dll")]
  49. public static extern Boolean AllocConsole();
  50. [DllImport("kernel32.dll")]
  51. public static extern Boolean FreeConsole();
  52. #endregion
  53. /// <summary>
  54. /// 获取输出颜色
  55. /// </summary>
  56. /// <param name="type">输出类型</param>
  57. /// <returns></returns>
  58. private ConsoleColor GetColor(LogType type)
  59. {
  60. switch (type)
  61. {
  62. case LogType.v: return ConsoleColor.Gray;
  63. case LogType.d: return ConsoleColor.Blue;
  64. case LogType.i: return ConsoleColor.Green;
  65. case LogType.w: return ConsoleColor.Yellow;
  66. case LogType.e: return ConsoleColor.Red;
  67. default: return ConsoleColor.Gray;
  68. }
  69. }
  70. /// <summary>
  71. /// 写出到控制台
  72. /// </summary>
  73. /// <param name="type">类型</param>
  74. /// <param name="tag">标记</param>
  75. /// <param name="message">消息</param>
  76. private void Write(LogType type, string message)
  77. {
  78. Console.ForegroundColor = GetColor(type);
  79. Console.WriteLine(LogFormat, DateTime.Now.ToString(TimeFormat), type.ToString(), message);
  80. if (IsWriteFile) WriteFile(type, message);
  81. }
  82. private void WriteFile(LogType type, string message)
  83. {
  84. if (IsWriteFile)
  85. {
  86. lock (LogFileLock)
  87. {
  88. //设置日志目录
  89. string logPath = AppDomain.CurrentDomain.BaseDirectory + LogPath;
  90. string file = string.Format(@"{0}\{1}.txt", logPath, DateTime.Now.ToString("yyyy-MM-dd"));
  91. //创建日志目录
  92. DirTool.Create(logPath);
  93. //写出日志
  94. TxtTool.Append(file, string.Format(LogFormat, DateTime.Now.ToString(TimeFormat), type.ToString(), message));
  95. }
  96. }
  97. }
  98. #region 分类详细输出
  99. /// <summary>
  100. /// 输出 verbose (啰嗦信息)
  101. /// </summary>
  102. /// <param name="message">消息</param>
  103. /// <param name="tag">可选:标记</param>
  104. public void v<T>(T msg)
  105. {
  106. if ((LogLevel & LogLevel.Verbose) == LogLevel.Verbose)
  107. Write(LogType.v, msg.ToString());
  108. }
  109. /// <summary>
  110. /// 输出 Debug (调试信息)
  111. /// </summary>
  112. /// <param name="message">消息</param>
  113. /// <param name="tag">可选:标记</param>
  114. public void d<T>(T msg)
  115. {
  116. if ((LogLevel & LogLevel.Debug) == LogLevel.Debug)
  117. Write(LogType.d, msg.ToString());
  118. }
  119. /// <summary>
  120. /// 输出 Information (重要信息)
  121. /// </summary>
  122. /// <param name="message">消息</param>
  123. /// <param name="tag">可选:标记</param>
  124. public void i<T>(T msg)
  125. {
  126. if ((LogLevel & LogLevel.Information) == LogLevel.Information)
  127. Write(LogType.i, msg.ToString());
  128. }
  129. /// <summary>
  130. /// 输出 Warning (警告信息)
  131. /// </summary>
  132. /// <param name="message">消息</param>
  133. /// <param name="tag">可选:标记</param>
  134. public void w<T>(T msg)
  135. {
  136. if ((LogLevel & LogLevel.Warning) == LogLevel.Warning)
  137. Write(LogType.w, msg.ToString());
  138. }
  139. /// <summary>
  140. /// 输出 Error (错误信息)
  141. /// </summary>
  142. /// <param name="message">消息</param>
  143. /// <param name="tag">可选:标记</param>
  144. public void e<T>(T msg)
  145. {
  146. if ((LogLevel & LogLevel.Error) == LogLevel.Error)
  147. Write(LogType.e, msg.ToString());
  148. }
  149. #endregion
  150. }
  151. }