c3b2a84e7e630747c4d4a0bc79c97ae2cdd9531b.svn-base 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace STMes
  6. {
  7. public interface ILogService
  8. {
  9. void WriteLog(string message);
  10. }
  11. public class CoreWriteLogFile : ILogService
  12. {
  13. private static object lockObj = new object();
  14. private string _postfix = "log";
  15. private bool _isLoging = false;
  16. public CoreWriteLogFile()
  17. {
  18. _postfix = "log";
  19. }
  20. public CoreWriteLogFile(string postfix)
  21. {
  22. if (postfix.Trim() == "")
  23. {
  24. _postfix = "log";
  25. }
  26. else
  27. {
  28. _postfix = postfix.Trim();
  29. }
  30. }
  31. public void StartLogging(bool startIt)
  32. {
  33. _isLoging = startIt;
  34. }
  35. #region ILogService ³ÉÔ±
  36. public void WriteLog(string message)
  37. {
  38. WriteLog(message, LogInfoLevel.Message, "", "");
  39. }
  40. public void WriteLog(string message, LogInfoLevel level)
  41. {
  42. WriteLog(message, level, "", "");
  43. }
  44. public void WriteLog(string message, string UserID, string codeInfo)
  45. {
  46. WriteLog(message, LogInfoLevel.Message, UserID, codeInfo);
  47. }
  48. public void WriteLog(string message, LogInfoLevel level, string UserID, string codeInfo)
  49. {
  50. if (!_isLoging) return;
  51. lock (lockObj)
  52. {
  53. try
  54. {
  55. string logFile = System.Environment.CurrentDirectory + "/log/" + "DBProxy_" + DateTime.Now.ToString("yyyyMMdd") + "_" + _postfix + ".log";
  56. FileInfo fi = new FileInfo(logFile);
  57. StreamWriter sw = null;
  58. try
  59. {
  60. if (fi.Exists)
  61. sw = fi.AppendText();
  62. else
  63. sw = fi.CreateText();
  64. string str = "";
  65. if (level == LogInfoLevel.Error)
  66. str = ">>> Error <<<";
  67. else
  68. str = ">>> Message <<<";
  69. string title = ">>> " + DateTime.Now + " <<<";
  70. title += str;
  71. if (UserID != "")
  72. {
  73. title += ">>> " + UserID + " <<<";
  74. }
  75. if (codeInfo != "")
  76. {
  77. title += ">>> " + codeInfo + " <<<";
  78. }
  79. sw.WriteLine(title);
  80. sw.WriteLine("{0}", message);
  81. sw.WriteLine();
  82. sw.Flush();
  83. }
  84. finally
  85. {
  86. if (sw != null)
  87. sw.Close();
  88. }
  89. }
  90. catch
  91. {
  92. // do nothing.
  93. }
  94. }
  95. }
  96. #endregion
  97. }
  98. public enum LogInfoLevel
  99. {
  100. Error = 1,
  101. Message = 2
  102. }
  103. }