日志工具类—LogUtil

作者:追风剑情 发布于:2020-9-3 10:42 分类:C#

  1. using System;
  2. using System.Text;
  3. using System.IO;
  4.  
  5. namespace UNetwork
  6. {
  7. /// <summary>
  8. /// 日志工具类
  9. /// </summary>
  10. public sealed class LogUtil
  11. {
  12. private static object lockObj = new object();
  13.  
  14. //日志分类
  15. public enum LogType
  16. {
  17. Info, //信息
  18. Trace, //追踪
  19. Warn, //警告
  20. Error, //错误
  21. Fatal //严重错误
  22. }
  23.  
  24. //输出设备
  25. public enum LogDevice
  26. {
  27. Console, //输出到控制台
  28. File, //输出到日志文件
  29. ConsoleAndFile //输出到控制台和日志文件
  30. }
  31.  
  32. private static string LogFileName
  33. {
  34. get
  35. {
  36. string timeFormat = "yyyyMMddHH";
  37. string timeStr = DateTime.Now.ToString(timeFormat);
  38. return timeStr;
  39. }
  40. }
  41.  
  42. private static string CurrentTimeStr
  43. {
  44. get
  45. {
  46. string timeFormat = "yyyy-MM-dd HH:mm:ss";
  47. string timeStr = DateTime.Now.ToString(timeFormat);
  48. return timeStr;
  49. }
  50. }
  51.  
  52. private static string LogFilePath(LogType logType)
  53. {
  54. string logDirectory = "./Log";
  55. string logClassify = string.Format("{0}/{1}", logDirectory, logType.ToString());
  56. string logFile = string.Format("{0}/{1}.txt", logClassify, LogFileName);
  57. try
  58. {
  59. if (!Directory.Exists(logDirectory))
  60. Directory.CreateDirectory(logDirectory);
  61. if (!Directory.Exists(logClassify))
  62. Directory.CreateDirectory(logClassify);
  63. }
  64. catch(Exception ex)
  65. {
  66. Console.WriteLine("Create Directory Failured\n{0}\n{1}", ex.Message, ex.StackTrace);
  67. }
  68. return logFile;
  69. }
  70.  
  71. private static void Write(string log, LogType logType=LogType.Info, LogDevice logDevice=LogDevice.Console)
  72. {
  73. lock (lockObj)
  74. {
  75. //文字颜色
  76. switch (logType)
  77. {
  78. case LogType.Fatal:
  79. case LogType.Error:
  80. Console.ForegroundColor = ConsoleColor.Red;
  81. break;
  82. case LogType.Trace:
  83. Console.ForegroundColor = ConsoleColor.Green;
  84. break;
  85. case LogType.Warn:
  86. Console.ForegroundColor = ConsoleColor.Yellow;
  87. break;
  88. default:
  89. Console.ForegroundColor = ConsoleColor.White;
  90. break;
  91. }
  92.  
  93. //是否需要输出到控制台
  94. string content = string.Format("[{0}] {1}\r\n", CurrentTimeStr, log);
  95. switch (logDevice)
  96. {
  97. case LogDevice.Console:
  98. Console.Write(content);
  99. break;
  100. case LogDevice.File:
  101.  
  102. break;
  103. case LogDevice.ConsoleAndFile:
  104. Console.Write(content);
  105. break;
  106. }
  107. Console.ForegroundColor = ConsoleColor.White;
  108.  
  109. //是否需要输出到日志文件
  110. if (logDevice == LogDevice.File || logDevice == LogDevice.ConsoleAndFile)
  111. {
  112. string logFilePath = LogFilePath(logType);
  113. try
  114. {
  115. File.AppendAllText(logFilePath, content, Encoding.UTF8);
  116. }
  117. catch (Exception ex)
  118. {
  119. Console.WriteLine("Write Log Failured\n{0}\n{1}", ex.Message, ex.StackTrace);
  120. }
  121. }
  122. }
  123. }
  124.  
  125. /// <summary>
  126. /// 写入一般信息
  127. /// </summary>
  128. /// <param name="log"></param>
  129. public static void Info(string log, LogDevice logDevice = LogDevice.Console)
  130. {
  131. Write(log, LogType.Info, logDevice);
  132. }
  133.  
  134. /// <summary>
  135. /// 写入追踪日志 (记录程序执行流程)
  136. /// </summary>
  137. /// <param name="log"></param>
  138. public static void Trace(string log, LogDevice logDevice = LogDevice.Console)
  139. {
  140. Write(log, LogType.Trace, logDevice);
  141. }
  142.  
  143. /// <summary>
  144. /// 写入出错日志 (指会导致程序无法按预期执行的错误)
  145. /// </summary>
  146. /// <param name="log"></param>
  147. public static void Error(string log, LogDevice logDevice = LogDevice.Console)
  148. {
  149. Write(log, LogType.Error, logDevice);
  150. }
  151.  
  152. /// <summary>
  153. /// 致命错误 (需要结束应用程序的错误)
  154. /// </summary>
  155. /// <param name="log"></param>
  156. public static void Fatal(string log, LogDevice logDevice = LogDevice.Console)
  157. {
  158. Write(log, LogType.Fatal, logDevice);
  159. }
  160.  
  161. /// <summary>
  162. /// 写入警告日志
  163. /// </summary>
  164. /// <param name="log"></param>
  165. /// <param name="logDevice"></param>
  166. public static void Warn(string log, LogDevice logDevice = LogDevice.Console)
  167. {
  168. Write(log, LogType.Warn, logDevice);
  169. }
  170. }
  171. }

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号