LogWriter.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace AwInitilizer.ProcedureLog
  7. {
  8. public class LogWriter<T1,T2> where T1 : Procedure.ProcedureBase where T2 : Enum
  9. {
  10. public Dictionary<T2, string> ReportPair { get; set; }
  11. public Dictionary<T2, string> LogPair { get; set; }
  12. internal readonly T1 source;
  13. public LogWriter(T1 source)
  14. {
  15. this.source = source;
  16. }
  17. public void Report(T2 logEvent, string data,bool isError = false)
  18. {
  19. string log = "";
  20. log = CreateLogMessage(logEvent, data);
  21. Log(log, isError);
  22. if (ReportPair != null && ReportPair.ContainsKey(logEvent))
  23. {
  24. MesReport(ReportPair[logEvent], data, !isError);
  25. }
  26. }
  27. public void Report(string logEvent, string data, bool isError = false)
  28. {
  29. string log = "";
  30. log = string.Format("{0} {1}", logEvent, data);
  31. Log(log, isError);
  32. MesReport(logEvent, data, !isError);
  33. }
  34. public virtual void Log(string data, bool isError = false, bool isDebugLog = false)
  35. {
  36. WriteMyLog(data);
  37. if (!isDebugLog)
  38. {
  39. WriteShortLog(data, isError);
  40. WriteFormateLog(data);
  41. }
  42. }
  43. private string CreateLogMessage(T2 logEvent, string data)
  44. {
  45. string log;
  46. if (LogPair != null && LogPair.ContainsKey(logEvent))
  47. {
  48. log = string.Format(LogPair[logEvent], data);
  49. }
  50. else
  51. {
  52. log = string.Format("{0} {1}", Enum.GetName(typeof(T2), logEvent), data);
  53. }
  54. return log;
  55. }
  56. private void MesReport(string key,string value, bool isSuccess)
  57. {
  58. //source.MesLogData[key] = value;
  59. source.MesLogData.Add(key,value,isSuccess);
  60. }
  61. private void WriteMyLog(string log)
  62. {
  63. source.InfoLog += string.Format("{0}\n", log);
  64. }
  65. private void WriteShortLog(string log, bool isError = false)
  66. {
  67. if (log.Length < 100)
  68. {
  69. Procedure.ProcedureBase.Logger.Print(log, isError);
  70. }
  71. }
  72. private void WriteFormateLog(string log)
  73. {
  74. source.ReportLog.Add(log);
  75. }
  76. }
  77. }