LogManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace HXX.Scanner.Copier
  7. {
  8. /// <summary>
  9. /// 日志类
  10. /// </summary>
  11. public class LogManager
  12. {
  13. public static readonly string ServerLogPath = @"C:\HXX\";
  14. public static readonly string ServerLogExtension = "-Log.txt";
  15. public static readonly string ServerLogDateFormat = "yyyy-MM-dd";
  16. private static DateTime CurrentDate = DateTime.Now.Date;
  17. private static string ServerLogFileName = string.Empty;
  18. public static void WriteLog(string Message)
  19. {
  20. WriteLog(Message, null);
  21. }
  22. public static void WriteLog(Exception ee)
  23. {
  24. WriteLog(ee.Message, ee);
  25. }
  26. public static void WriteLog(string Message, Exception ee)
  27. {
  28. try
  29. {
  30. CheckLogFile();
  31. using (StreamWriter sw = new StreamWriter(ServerLogFileName, true))
  32. {
  33. string Content = string.Empty;
  34. Content += "=======================================================================================================" + Environment.NewLine;
  35. Content += "************** Time At:" + DateTime.Now.ToString(@"[yyyy-MM-dd HH:mm:ss]") + Environment.NewLine;
  36. Content += "************** Description:" + Environment.NewLine + Message + Environment.NewLine;
  37. if (ee != null)
  38. {
  39. Content += ee.Message + Environment.NewLine;
  40. Content += "************** Error Stack Trace:" + Environment.NewLine + ee.StackTrace + Environment.NewLine;
  41. Content += "************** Error Source:" + ee.Source + Environment.NewLine;
  42. }
  43. Content += "=======================================================================================================" + Environment.NewLine;
  44. Content += Environment.NewLine;
  45. Content += Environment.NewLine;
  46. sw.Write(Content);
  47. }
  48. }
  49. catch { }
  50. }
  51. private static void CheckLogFile()
  52. {
  53. if (CurrentDate != DateTime.Now.Date || ServerLogFileName == string.Empty)
  54. {
  55. CurrentDate = DateTime.Now.Date;
  56. ServerLogFileName = DateTime.Now.ToString(ServerLogDateFormat) + ServerLogExtension;
  57. ServerLogFileName = ServerLogPath + ServerLogFileName;
  58. }
  59. if (!Directory.Exists(ServerLogPath))
  60. {
  61. Directory.CreateDirectory(ServerLogPath);
  62. }
  63. }
  64. public static void WriteLogCoco(string Title, List<Exception> ExceptionList)
  65. {
  66. try
  67. {
  68. CheckLogFile();
  69. using (StreamWriter sw = new StreamWriter(ServerLogFileName, true))
  70. {
  71. string Content = string.Empty;
  72. Content += "=======================================================================================================" + Environment.NewLine;
  73. Content += "************** Time At:" + DateTime.Now.ToString(@"[yyyy-MM-dd HH:mm:ss]") + Environment.NewLine;
  74. Content += Title + Environment.NewLine;
  75. foreach (Exception ee in ExceptionList)
  76. {
  77. Content += "************** Description:" + Environment.NewLine + ee.Message + Environment.NewLine;
  78. if (ee != null)
  79. {
  80. Content += "************** Error Stack Trace:" + Environment.NewLine + ee.StackTrace + Environment.NewLine;
  81. Content += "************** Error Source:" + ee.Source + Environment.NewLine + Environment.NewLine;
  82. }
  83. }
  84. Content += "=======================================================================================================" + Environment.NewLine;
  85. Content += Environment.NewLine;
  86. Content += Environment.NewLine;
  87. sw.Write(Content);
  88. }
  89. }
  90. catch { }
  91. }
  92. }
  93. }