ExceptionsHelp.cs 7.29 KB
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Newtonsoft.Json;

namespace Hh.Mes.Common.log
{
    /// <summary>
    /// 异常类
    /// </summary>
    public class ExceptionsHelp
    {
        private static volatile ExceptionsHelp _process = null;
        private static readonly object LockHelper = new object();

        public readonly string ReturnMsg500 = "{\"Code\":500,\"Status\":fasle,\"Message\":\"api 500 error\",\"Result\":null}";
        public readonly string ReturnMsgList500 = "{\"code\":-1,\"count\":0,\"msg\":\"api 500 error\",\"data\":null}";
        private ExceptionsHelp()
        {

        }

        /// <summary> Gets the instance
        /// </summary>
        public static ExceptionsHelp Instance
        {
            get
            {
                if (_process == null)
                {
                    lock (LockHelper)
                    {
                        if (_process == null)
                        {
                            _process = new ExceptionsHelp();
                        }
                    }
                }
                return _process;
            }
        }

        /// <summary>异常日志  
        /// </summary>
        /// <param name="action">action</param>
        /// <param name="methodName">methodName</param>
        /// <param name="actionFinally">异常finally 执行动作(用于销毁、释放对象等)</param>
        public void ExecuteVoidFunc(Action action, string methodName = "", Action actionFinally = null, string catchRetrunValue = "action")
        {
            if (action == null) return;
            string tempMethodName = (methodName + "").Length == 0 ? action.Method.Name : methodName;
            try
            {
                action.Invoke();
            }
            catch (System.Threading.ThreadAbortException)
            {
                //不作处理 (合同打印触发异常)
            }
            catch (AggregateException ex)
            {
                Log4NetHelper.Instance.Error($"【{tempMethodName}】" + ex.Message);
                //首先任务是并行计算的,处理过程中可能会产生n多的异常,普通的Exception并不能获取到异常,然而为并行诞生的AggregateExcepation就可以获取到一组异常。
                foreach (var single in ex.InnerExceptions)
                {

                }
            }
            catch (Exception ex)
            {
                Log4NetHelper.Instance.Error($"【{tempMethodName}】" + ex.Message);
                throw new Exception(ex.Message);
            }
            finally
            {
                actionFinally?.Invoke();
            }
        }

        public string ExecuteString(Func<string> action, string catchRetrunValue = "action")
        {
            try
            {
                return action.Invoke();
            }
            catch (Exception ex)
            {
                Log4NetHelper.Instance.Error(ex.Message);
                return ReturnValue(ex, catchRetrunValue);
            }
        }

        /// <summary>异常返回 ReturnMsg500
        /// </summary>
        /// <typeparam name="T">类型T</typeparam>
        /// <param name="action">action</param>
        /// <param name="methodName">methodName</param>
        /// <param name="actionFinally"></param>
        /// <param name="actionCatCh"></param>
        /// <param name="catchRetrunValue">action 新增编辑删除, list 是layui table 返回格式</param>
        /// <returns></returns>
        public dynamic ExecuteT<T>(Func<T> action, string methodName = "", Action actionFinally = null,Action actionCatCh=null, string catchRetrunValue = "action")
        {
            //var s = Activator.CreateInstance<T>();
            //var defaultType = default(T);
            string tempMethodName = (methodName + "").Length == 0 ? action.Method.Name : methodName;
            if (action == null) return ReturnMsg500;
            try
            {
                return action.Invoke();
            }
            catch (ThreadAbortException ex)
            {
                actionCatCh?.Invoke();
                //不作处理
                return ReturnValue(ex, catchRetrunValue);
            }
            catch (AggregateException ex)
            {
                actionCatCh?.Invoke();
                //首先任务是并行计算的,处理过程中可能会产生n多的异常,普通的Exception并不能获取到异常,然而为并行诞生的AggregateExcepation就可以获取到一组异常。
                foreach (var single in ex.InnerExceptions)
                {
                    Log4NetHelper.Instance.Error($"【{tempMethodName}】" + single.Message);
                }
                return ReturnValue(ex, catchRetrunValue);
            }
            catch (Exception ex)
            {
                actionCatCh?.Invoke();
                Log4NetHelper.Instance.Error($"【{tempMethodName}】" + ex.Message);
                return ReturnValue(ex, catchRetrunValue);
            }
            finally
            {
                actionFinally?.Invoke();
            }
        }


        /// <summary>异常返回 T
        /// </summary>
        /// <typeparam name="T">类型T</typeparam>
        /// <param name="action">action</param>
        /// <param name="methodName">methodName</param>
        /// <param name="actionFinally"></param>
        /// <returns></returns>
        public T Execute<T>(Func<T> action, string methodName = "", Action actionFinally = null)
        {
            var defaultType = default(T);
            string tempMethodName = (methodName + "").Length == 0 ? action.Method.Name : methodName;
            if (action == null) return defaultType;
            try
            {
                return action.Invoke();
            }
            catch (System.Threading.ThreadAbortException)
            {
                //不作处理
                return defaultType;
            }
            catch (AggregateException ex)
            {
                //首先任务是并行计算的,处理过程中可能会产生n多的异常,普通的Exception并不能获取到异常,然而为并行诞生的AggregateExcepation就可以获取到一组异常。
                foreach (var single in ex.InnerExceptions)
                {
                    Log4NetHelper.Instance.Error($"【{tempMethodName}】" + single.Message);
                }
                //日志
                return defaultType;
            }
            catch (Exception ex)
            {
                Log4NetHelper.Instance.Error($"【{tempMethodName}】" + ex.Message);
                //日志
                return defaultType;
            }
            finally
            {
                actionFinally?.Invoke();
            }
        }

        /// <summary>
        /// 异常返回信息 action 是普通操作,list 是layui table 返回格式
        /// </summary>
        private string ReturnValue(Exception ex, string catchRetrunValue)
        {
            var tempMes = ex.ToString().StrJsonReplace();
            tempMes = tempMes.Substring(1, 160);
            return catchRetrunValue == "action" ? "{\"Code\":500,\"Status\":false,\"Message\":\"" + tempMes + "\",\"Result\":null}"
                : "{\"code\":-1,\"count\":0,\"msg\":\"" + tempMes + "\",\"data\":null}";
        }

    }
}