84f3a4daf06d63659248cc4b44bb216860fb3098.svn-base 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Core.Mes.ServerCommon
  6. {
  7. public interface ILogService
  8. {
  9. void WriteLog(DateTime dt, string vLoginID, string vUserName, string vUSERHOSTNAME, string vUSERIPADRESS, string vOPerateType,
  10. string vControlTYPE, string vOPERATEOBJECT, string vFormName, string vClassName, string vREMARK);
  11. }
  12. public class CoreWriteLogFile
  13. {
  14. private static object lockObj = new object();
  15. public static void WriteLog(DateTime dt, string vLoginID, string vUserName, string vUSERHOSTNAME, string vUSERIPADRESS, string vOPerateType,
  16. string vControlTYPE, string vOPERATEOBJECT, string vFormName, string vClassName, string vREMARK)
  17. {
  18. lock (lockObj)
  19. {
  20. try
  21. {
  22. string filePath = System.Environment.CurrentDirectory + @"\log\UserEvents\";
  23. if (!Directory.Exists(filePath))
  24. {
  25. Directory.CreateDirectory(filePath);
  26. }
  27. string logFile = filePath + "UE_" + dt.ToString("yyyyMMdd") + "_LOG" + ".log";
  28. FileInfo fi = new FileInfo(logFile);
  29. StreamWriter sw = null;
  30. try
  31. {
  32. string sContent = "";
  33. if (fi.Exists)
  34. {
  35. sw = fi.AppendText();
  36. }
  37. else
  38. {
  39. sw = fi.CreateText();
  40. sContent = string.Format("{10,23}, {0,10}, {1,10}, {2,20}, {3,15}, {4,5}, {5,10}, {6,-10}, [{7}], [{8}], [{9}]", " 登录ID", "用户名", "主机名", "主机IP", "操作类型", "控件类型", "控件名次", "窗口名称", "窗口类型", "备注", "时间");
  41. sw.WriteLine(sContent);
  42. }
  43. sContent = string.Format("{0,10}, {1,10}, {2,20}, {3,15}, {4,5}, {5,10}, {6,-10}, [{7}], [{8}], [{9}]", vLoginID, vUserName, vUSERHOSTNAME, vUSERIPADRESS, vOPerateType, vControlTYPE, vOPERATEOBJECT, vFormName, vClassName, vREMARK);
  44. string sc = string.Format("{0}, {1}", dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), sContent);
  45. sw.WriteLine(sc.Trim());
  46. sw.Flush();
  47. }
  48. finally
  49. {
  50. if (sw != null)
  51. sw.Close();
  52. }
  53. }
  54. catch
  55. {
  56. // do nothing.
  57. }
  58. }
  59. }
  60. }
  61. }