| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 | using System;using System.Collections;using System.Collections.Concurrent;using System.Linq.Expressions;using System.Threading;namespace WCS.Core{    /// <summary>    /// Rdeis 数据上抛    /// </summary>    public static class Ltc    {        private static readonly ConcurrentDictionary<Thread, string> 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>(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<T>(Expression<Func<T>> 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, T>(T1 obj, Expression<Func<T1, T>> 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, T2, T>(T1 obj, T2 obj2, Expression<Func<T1, T2, T>> 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, T2, T3, T>(T1 obj, T2 obj2, T3 obj3, Expression<Func<T1, T2, T3, T>> 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);            }        }    }}
 |