Ejemplo n.º 1
0
        public static void LogAndStatistics(int result, Net.Protocol p, bool IsRequestSaved)
        {
            var actionName = p.GetType().FullName;

            if (IsRequestSaved == false)
            {
                actionName = actionName + ":Response";
            }

            if (result != 0)
            {
                var logLevel = (null != p.Service.Zeze)
                    ? p.Service.Zeze.Config.ProcessReturnErrorLogLevel
                    : NLog.LogLevel.Info;

                logger.Log(logLevel,
                           "Task {0} Return={1}@{2}:{3} UserState={4}",
                           actionName,
                           result,
                           Zeze.Net.Protocol.GetModuleId(result),
                           Zeze.Net.Protocol.GetProtocolId(result),
                           p.UserState);
            }
#if ENABLE_STATISTICS
            ProcedureStatistics.Instance.GetOrAdd(actionName).GetOrAdd(result).IncrementAndGet();
#endif
        }
Ejemplo n.º 2
0
        public static int Call(Func <int> func, Net.Protocol p,
                               Action <Net.Protocol, int> actionWhenError = null)
        {
            bool IsRequestSaved = p.IsRequest; // 记住这个,以后可能会被改变。

            try
            {
                int result = func();
                if (result != 0 && IsRequestSaved)
                {
                    actionWhenError?.Invoke(p, result);
                }
                LogAndStatistics(result, p, IsRequestSaved);
                return(result);
            }
            catch (Exception ex)
            {
                while (ex is AggregateException)
                {
                    ex = ex.InnerException;
                }

                var errorCode = ex is TaskCanceledException
                    ? Procedure.CancelExcption
                    : Procedure.Excption;

                if (IsRequestSaved)
                {
                    actionWhenError?.Invoke(p, errorCode);
                }

                LogAndStatistics(errorCode, p, IsRequestSaved);
                // 使用 InnerException
                logger.Error(ex, "Task {0} Exception UserState={1}",
                             p.GetType().FullName, p.UserState);
                return(errorCode);
            }
        }