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 readonly ConcurrentDictionary Channels = new(); 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"; } switch (obj) { case bool: { var b = obj as bool?; return b.Value ? "成立" : "不成立"; } case ICollection collection: { return collection.Count + "元素"; } } 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) { 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); } } } }