LogWriter.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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);
  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);
  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)
  57. {
  58. if (source.LogPair.ContainsKey(key))
  59. source.LogPair[key] = value;
  60. else
  61. source.LogPair.Add(key, value);
  62. }
  63. private void WriteMyLog(string log)
  64. {
  65. source.InfoLog += string.Format("{0}\n", log);
  66. }
  67. private void WriteShortLog(string log, bool isError = false)
  68. {
  69. if (log.Length < 100)
  70. {
  71. Procedure.ProcedureBase.Logger.Print(log, isError);
  72. }
  73. }
  74. private void WriteFormateLog(string log)
  75. {
  76. source.ReportLog.Add(log);
  77. }
  78. }
  79. }