Esempio n. 1
0
        public void OnActionExecuted(ActionExecutedContext context)
        {
            _logRequest.ExcuteEndTime = DateTime.Now;

            if (context.Exception == null)
            {
                _logRequest.ResponseBodys = (context.Result as ObjectResult).Value.ToJson();
                LogSuccess();
            }
            else
            {
                Result result = null;
                if (context.Exception is ChecksException ce) //验证异常(业务)
                {
                    result = Result.Fail(ce.Code, ce.Message);
                    _logRequest.ResponseBodys = result.ToJson();
                    LogSuccess();
                }
                else if (context.Exception is QueueException qe) //消息队列异常(业务)
                {
                    result = Result.Fail(qe.Code, qe.Message);
                    _logRequest.ResponseBodys = result.ToJson();
                    LogSuccess();
                }
                else
                {
                    _logRequest.ResponseBodys = context.Exception.ToString();
                    result = Result.Fail(500, "服务器连接错误");
                    Log.Error(new LogFormat(_logRequest.ToJson(), "services.pay", "http", "controller", 500, (_logRequest.ExcuteEndTime - _logRequest.ExcuteStartTime).Milliseconds, "", _logRequest.Url, ""));
                }
                context.ExceptionHandled = true;
                context.Result           = new ObjectResult(result.ToJson());
            }
        }
 private bool SaveLog(string queueName, LogRequest logRequest)
 {
     try
     {
         if (queueName.Equals(LogLevel.Info.ToString()))
         {
             _InfoLogAccess.AddLog(logRequest);
             return(true);
         }
         if (queueName.Equals(LogLevel.Error.ToString()))
         {
             _ErrorLogAccess.AddLog(logRequest);
             return(true);
         }
         return(false);
     }
     catch (Exception ex)
     {
         Logger.Error($"{nameof(logRequest.AppId)}:{logRequest.AppId} 保存日志发生错误:{ex}  日志内容:{logRequest.ToJson()}");
         return(false);
     }
 }