/// <summary> /// Read Line Event /// </summary> /// <param name="line">line</param> protected virtual bool OnReadLine(string line) { bool result = true; foreach (var item in arrExitCmd) { if (string.Compare(line, item, ignoreCase) == 0) { OnExit(); result = false; break; } } if (result) { try { OnCmd(line); } catch (Exception e) { ConsoleHelper.WriteLine(ELogCategory.Fatal, "OnCmd Error"); CommonLogger.WriteLog(ELogCategory.Fatal, "OnCmd Error", e); } ProcessDetailCmd(line); } return(result); }
/// <summary> /// ProcessDetailCmd /// </summary> /// <param name="param"></param> protected virtual void ProcessDetailCmd(DetailCmdParam param) { Type t = typeof(IDetailCmd); List <Type> lsType = new List <Type>(); foreach (var item in DetailCmdAssemblies) { Type[] arr = item.GetTypes(); lsType.AddRange(arr); } Type[] types = lsType.ToArray(); List <Type> ls = new List <Type>(); foreach (var item in types) { Type[] ts = item.GetInterfaces(); if (ts != null && ts.Any(x => x == t)) { ls.Add(item); } } if (!ls.Any()) { ConsoleHelper.WriteLine(ELogCategory.Warn, string.Format("Undefined Cmd: {0}", param.Cmd)); } else { bool bExist = false; foreach (var item in ls) { object o = Activator.CreateInstance(item); IDetailCmd obj = o as IDetailCmd; if (obj != null) { if (obj.Cmd.Equals(param.Cmd)) { bExist = true; try { obj.OnDetailCmd(param.Params); } catch (Exception e) { ConsoleHelper.WriteLine(ELogCategory.Fatal, "OnDetailCmd error"); CommonLogger.WriteLog(ELogCategory.Fatal, "OnDetailCmd Exception", e); } } } } if (!bExist) { ConsoleHelper.WriteLine(ELogCategory.Warn, string.Format("Undefined Cmd: {0}", param.Cmd)); } } }
protected virtual void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) { CommonLogger.WriteLog(ELogCategory.Fatal, e: e.ExceptionObject as Exception); if (e.IsTerminating) { Environment.Exit(1); } }
protected virtual void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) { var e1 = e.ExceptionObject as Exception; CommonLogger.WriteLog( ELogCategory.Fatal, string.Format("ConsoleInit.OnUnhandledException: {0}", e1.Message), e: e1 ); if (e.IsTerminating) { Environment.Exit(1); } }
protected virtual void ProcessHttpRequest(HttpListenerContext ctx) { CommonLogger.WriteLog( ELogCategory.Info, string.Format("Recv http request, path: {0}, remote: {1}", ctx.Request.Url.PathAndQuery, ctx.Request.RemoteEndPoint.Address.ToString() ) ); var session = CreateHttpRequestSession(ctx); ProcessRequest.Invoke(); }
/// <summary> /// Write Line /// </summary> /// <param name="categeory">categeory</param> /// <param name="log">log</param> /// <param name="isWriteFile">is Write to File</param> /// <param name="showDate">showDate</param> /// <param name="showCategory">showCategory</param> /// <param name="e">Exception</param> public static void WriteLine(ELogCategory categeory, string log, bool isWriteFile = false, bool showDate = true, bool showCategory = false, Exception e = null) { string str = ""; if (showDate) { str += string.Format("[{0}] ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } if (showCategory) { str += string.Format("[{0}] ", categeory); } str += log; ConsoleColor color = ConsoleColor.Gray; switch (categeory) { case ELogCategory.Debug: color = ConsoleColor.DarkGray; break; case ELogCategory.Warn: color = ConsoleColor.DarkYellow; break; case ELogCategory.Error: color = ConsoleColor.DarkRed; break; case ELogCategory.Fatal: color = ConsoleColor.Red; break; } Console.ForegroundColor = color; Console.WriteLine(str); Console.ResetColor(); if (isWriteFile) { CommonLogger.WriteLog(categeory, log, e); } }
/// <summary> /// Process Detail Cmd /// </summary> /// <param name="cmd"></param> protected virtual void ProcessDetailCmd(string cmd) { if (!string.IsNullOrEmpty(cmd)) { string[] arr = cmd.Split(' '); DetailCmdParam param = new Common.DetailCmdParam(); param.Cmd = ""; param.Params = new Dictionary <string, string>(); if (arr.Length > 0) { param.Cmd = arr[0]; bool bException = false; try { Regex reg = new Regex("\\s+-([a-zA-Z0-9]{1,15})", RegexOptions.Multiline); MatchCollection mc = reg.Matches(cmd); for (int i = 0; i < mc.Count; i++) { string key = mc[i].Value.Replace(" ", "").Replace("-", ""); int startIndex = mc[i].Index + mc[i].Length; int length = 0; if (i + 1 < mc.Count) { length = mc[i + 1].Index - mc[i].Index - mc[i].Length; } else { length = cmd.Length - mc[i].Index - mc[i].Length; } string value = cmd.Substring(startIndex, length).Trim(); param.Params.Add(key, value); } } catch (Exception e) { bException = true; ConsoleHelper.WriteLine( ELogCategory.Warn, string.Format("Invalid cmd!") ); CommonLogger.WriteLog( ELogCategory.Warn, string.Format("Invalid cmd: {0}", cmd), e ); } if (!bException) { try { OnDetailCmd(param); } catch (Exception e) { ConsoleHelper.WriteLine(ELogCategory.Fatal, "OnDetailCmd Error"); CommonLogger.WriteLog(ELogCategory.Fatal, "OnDetailCmd Error", e); } } } } }