LogHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Configuration;
  6. using System.IO;
  7. using CB.Entity;
  8. namespace CB.TrendTool
  9. {
  10. public class LogHelper
  11. {
  12. /// <summary>
  13. /// 日志的记录类型是Text或SQLServer
  14. /// </summary>
  15. private static string LogType
  16. {
  17. get
  18. {
  19. return "text";
  20. }
  21. }
  22. /// <summary>
  23. /// Text日志的存放路径
  24. /// </summary>
  25. private static string GetLogPath
  26. {
  27. get
  28. {
  29. return System.Configuration.ConfigurationManager.AppSettings["LogPath"].ToString();
  30. }
  31. }
  32. /// <summary>
  33. /// 记录日志信息
  34. /// </summary>
  35. /// <param name="lt">彩票种类实体,如果传入null对象将直接将日志存放到统一文件</param>
  36. /// <param name="MessgeInfo">异常信息</param>
  37. /// <param name="type">类型(枚举)</param>
  38. /// <param name="WriteTag">是否增加日志的分割线</param>
  39. public static void WriteLog(string MessgeInfo, ExceptionType type, bool WriteTag)
  40. {
  41. if (LogType == "SQLServer")
  42. {
  43. WriteLogToSQLServer(MessgeInfo, type, WriteTag);
  44. }
  45. else //默认都写入日志文件
  46. {
  47. WriteLogToText(MessgeInfo, type, WriteTag);
  48. }
  49. }
  50. /// <summary>
  51. /// 将日志记录写入文本文件
  52. /// </summary>
  53. /// <param name="lt"></param>
  54. /// <param name="MessgeInfo"></param>
  55. /// <param name="type"></param>
  56. /// <param name="WriteTag"></param>
  57. private static void WriteLogToText(string MessgeInfo, ExceptionType type, bool WriteTag)
  58. {
  59. string LogPath = "";
  60. if (string.IsNullOrEmpty(GetLogPath))
  61. {
  62. //实体为空记录日志
  63. LogPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "RunLog\\ServiceTooLog\\");
  64. }
  65. else
  66. {
  67. LogPath = GetLogPath;
  68. }
  69. if (!Directory.Exists(LogPath))
  70. {
  71. Directory.CreateDirectory(LogPath);
  72. }
  73. string fileName = LogPath + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
  74. StreamWriter stream = null;
  75. try
  76. {
  77. string log = "[" + System.DateTime.Now.ToString() + "]>> " + MessgeInfo;
  78. if (!File.Exists(fileName))
  79. {
  80. Stream stream2 = null;
  81. try
  82. {
  83. stream2 = File.Create(fileName);
  84. }
  85. finally
  86. {
  87. if (stream2 != null)
  88. {
  89. stream2.Close();
  90. stream2.Dispose();
  91. }
  92. }
  93. }
  94. stream = File.AppendText(fileName);
  95. if (WriteTag)
  96. {
  97. stream.WriteLine("*********************************************************************");
  98. }
  99. stream.WriteLine(log);
  100. }
  101. catch
  102. { }
  103. finally
  104. {
  105. if (stream != null)
  106. {
  107. stream.Close();
  108. stream.Dispose();
  109. }
  110. }
  111. }
  112. private static void WriteLogToSQLServer( string MessgeInfo, ExceptionType type, bool WriteTag)
  113. {
  114. }
  115. }
  116. public enum ExceptionType
  117. {
  118. /// <summary>
  119. /// 系统日志
  120. /// </summary>
  121. SystemLog = 0,
  122. /// <summary>
  123. /// 异常日志
  124. /// </summary>
  125. ExceptionLog = 1
  126. }
  127. }