be5167bac06513d432542771f98392629b96ea58.svn-base 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. public CoreWriteLogFile()
  16. {
  17. _postfix = "log";
  18. }
  19. public CoreWriteLogFile(string postfix)
  20. {
  21. if (postfix.Trim() == "")
  22. {
  23. _postfix = "log";
  24. }
  25. else
  26. {
  27. _postfix = postfix.Trim();
  28. }
  29. }
  30. #region ILogService ³ÉÔ±
  31. public void WriteLog(string message)
  32. {
  33. WriteLog(message, LogInfoLevel.Message, "", "");
  34. }
  35. public void WriteLog(string message, LogInfoLevel level)
  36. {
  37. WriteLog(message, level, "", "");
  38. }
  39. public void WriteLog(string message, string UserID, string codeInfo)
  40. {
  41. WriteLog(message, LogInfoLevel.Message, UserID, codeInfo);
  42. }
  43. public void WriteLog(string message, LogInfoLevel level, string UserID, string codeInfo)
  44. {
  45. lock (lockObj)
  46. {
  47. try
  48. {
  49. string logFile = System.Environment.CurrentDirectory + "/log/" + "DBProxy_" + DateTime.Now.ToString("yyyyMMdd") + "_" + _postfix + ".log";
  50. FileInfo fi = new FileInfo(logFile);
  51. StreamWriter sw = null;
  52. try
  53. {
  54. if (fi.Exists)
  55. sw = fi.AppendText();
  56. else
  57. sw = fi.CreateText();
  58. string str = "";
  59. if (level == LogInfoLevel.Error)
  60. str = ">>> Error <<<";
  61. else
  62. str = ">>> Message <<<";
  63. string title = ">>> " + DateTime.Now + " <<<";
  64. title += str;
  65. if (UserID != "")
  66. {
  67. title += ">>> " + UserID + " <<<";
  68. }
  69. if (codeInfo != "")
  70. {
  71. title += ">>> " + codeInfo + " <<<";
  72. }
  73. sw.WriteLine(title);
  74. sw.WriteLine("{0}", message);
  75. sw.WriteLine();
  76. sw.Flush();
  77. }
  78. finally
  79. {
  80. if (sw != null)
  81. sw.Close();
  82. }
  83. }
  84. catch
  85. {
  86. // do nothing.
  87. }
  88. }
  89. }
  90. #endregion
  91. }
  92. public enum LogInfoLevel
  93. {
  94. Error = 1,
  95. Message = 2
  96. }
  97. }