using System; using System.Collections; using System.Collections.Concurrent; using System.Linq.Expressions; using System.Threading; namespace WCS.Core { /// /// Rdeis 数据上抛 /// public static class Ltc { private static ConcurrentDictionary Channels = new ConcurrentDictionary(); public static void SetChannel(string channel) { Channels[Thread.CurrentThread] = channel; } public static string GetChannel() { return Channels[Thread.CurrentThread]; } public static void Log(string msg) { var channel = GetChannel(); if (channel == "刷新") { return; } DebugPublisher.Publish(GetChannel(), msg); } private static string ResultString(T obj) { if (obj == null) { return "null"; } else if (obj is bool) { var b = obj as Boolean?; return b.Value ? "成立" : "不成立"; } else if (obj is ICollection) { var coll = obj as ICollection; return coll.Count.ToString() + "元素"; } return obj.ToString(); } public static T Do(Expression> exp) { var msg = exp.ExpToString(); msg += " 结果:"; try { var res = exp.Compile().Invoke(); return res; } catch (Exception ex) { msg += ex.GetBaseException().Message; throw; } finally { Log(msg); } } /// /// 判断执行条件是否有效 /// /// /// /// 表达式需要用到的传参 /// 表达式 /// 焦点 /// /// public static T Do(T1 obj, Expression> exp,string message,string errorMessage) { var msg = ""; try { try { msg = exp.ExpToString(); } catch (Exception) { //TODO:日志记录 } msg += " 结果:"; var res = exp.Compile().Invoke(obj); msg += ResultString(res); return res; } catch (Exception ex) { msg += ex.GetBaseException().Message; throw; } finally { Log(msg); } } public static T Do(T1 obj, T2 obj2, Expression> exp) { var msg = exp.ExpToString(); msg += " 结果:"; try { var res = exp.Compile().Invoke(obj, obj2); msg += ResultString(res); return res; } catch (Exception ex) { msg += ex.GetBaseException().Message; throw; } finally { Log(msg); } } public static T Do(T1 obj, T2 obj2, T3 obj3, Expression> exp) { var msg = exp.ExpToString(); msg += " 结果:"; try { var res = exp.Compile().Invoke(obj, obj2, obj3); msg += ResultString(res); return res; } catch (Exception ex) { msg += ex.GetBaseException().Message; throw; } finally { Log(msg); } } } }