1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- 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);
- }
- }
- 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);
- }
- 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)
- {
- if (source.LogPair.ContainsKey(key))
- source.LogPair[key] = value;
- else
- source.LogPair.Add(key, value);
- }
- 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);
- }
- }
- }
|