123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace AwInitilizer.ProcedureLog
- {
- public class LogWriter<T1,T2> where T1 : Procedure.ProcedureBase where T2 : Enum
- {
- public Dictionary<T2, string> ReportPair { get; set; }
- public Dictionary<T2, string> LogPair { get; set; }
- internal readonly T1 source;
- public LogWriter(T1 source)
- {
- this.source = source;
- }
- public void Report(T2 logEvent, string data,bool isError = false)
- {
- string log = "";
- log = CreateLogMessage(logEvent, data);
- Log(log, isError);
- if (ReportPair != null && ReportPair.ContainsKey(logEvent))
- {
- MesReport(ReportPair[logEvent], data, !isError);
- }
- }
- public void Report(string logEvent, string data, bool isError = false)
- {
- string log = "";
- log = string.Format("{0} {1}", logEvent, data);
- Log(log, isError);
- MesReport(logEvent, data, !isError);
- }
- public virtual void Log(string data, bool isError = false, bool isDebugLog = false)
- {
- WriteMyLog(data);
- if (!isDebugLog)
- {
- WriteShortLog(data, isError);
- WriteFormateLog(data);
- }
- }
- private string CreateLogMessage(T2 logEvent, string data)
- {
- string log;
- if (LogPair != null && LogPair.ContainsKey(logEvent))
- {
- log = string.Format(LogPair[logEvent], data);
- }
- else
- {
- log = string.Format("{0} {1}", Enum.GetName(typeof(T2), logEvent), data);
- }
- return log;
- }
- private void MesReport(string key,string value, bool isSuccess)
- {
- //source.MesLogData[key] = value;
- source.MesLogData.Add(key,value,isSuccess);
- }
- private void WriteMyLog(string log)
- {
- source.InfoLog += string.Format("{0}\n", log);
- }
- private void WriteShortLog(string log, bool isError = false)
- {
- if (log.Length < 100)
- {
- Procedure.ProcedureBase.Logger.Print(log, isError);
- }
- }
- private void WriteFormateLog(string log)
- {
- source.ReportLog.Add(log);
- }
- }
- }
|