コード例 #1
0
        private void HandlerException(ExceptionContext context, ResultDto apiResponse, int statusCode)
        {
            apiResponse.Request = LinCmsUtils.GetRequest(context.HttpContext);

            context.Result = new JsonResult(apiResponse)
            {
                StatusCode  = statusCode,
                ContentType = "application/json",
            };
            context.ExceptionHandled = true;
        }
コード例 #2
0
        /// <summary>
        /// 处理方式:返回Json格式
        /// </summary>
        /// <returns></returns>
        private async Task JsonHandle(HttpContext context, string errorMsg, ErrorCode errorCode, int statusCode)
        {
            ResultDto apiResponse = new ResultDto()
            {
                Msg       = errorMsg,
                ErrorCode = errorCode,
                Request   = LinCmsUtils.GetRequest(context)
            };;

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = statusCode;
            await context.Response.WriteAsync(apiResponse.ToString(), Encoding.UTF8);;
        }
コード例 #3
0
        private void HandlerException(ExceptionContext context, UnifyResponseDto apiResponse, int statusCode)
        {
            apiResponse.Request = LinCmsUtils.GetRequest(context.HttpContext);

            _logger.LogError(JsonConvert.SerializeObject(apiResponse));

            context.Result = new JsonResult(apiResponse)
            {
                StatusCode  = statusCode,
                ContentType = "application/json",
            };
            context.ExceptionHandled = true;
        }
コード例 #4
0
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            Stopwatch.Stop();
            //当方法或控制器上存在DisableAuditingAttribute特性标签时,不记录日志
            if (context.ActionDescriptor is ControllerActionDescriptor d && d.MethodInfo.IsDefined(typeof(DisableAuditingAttribute), true) ||
                context.Controller.GetType().IsDefined(typeof(DisableAuditingAttribute), true)
                )
            {
                base.OnActionExecuted(context);
                return;
            }

            LinLog linLog = new LinLog()
            {
                Method       = context.HttpContext.Request.Method,
                Path         = context.HttpContext.Request.Path,
                StatusCode   = context.HttpContext.Response.StatusCode,
                OtherMessage = $"参数:{ActionArguments}\n耗时:{Stopwatch.Elapsed.TotalMilliseconds} 毫秒"
            };

            ControllerActionDescriptor auditActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;

            AuditingLogAttribute auditingLogAttribute = auditActionDescriptor.GetCustomAttribute <AuditingLogAttribute>();

            if (auditingLogAttribute != null)
            {
                linLog.Message = auditingLogAttribute.Template;
            }

            LinCmsAuthorizeAttribute linCmsAttribute = auditActionDescriptor.GetCustomAttribute <LinCmsAuthorizeAttribute>();

            if (linCmsAttribute != null)
            {
                linLog.Authority = linCmsAttribute.Permission;
            }


            base.OnActionExecuted(context);

            if (context.Result is ObjectResult objectResult && objectResult.Value != null)
            {
                if (objectResult.Value.ToString().Contains("ErrorCode"))
                {
                    ResultDto resultDto = JsonConvert.DeserializeObject <ResultDto>(objectResult.Value.ToString());

                    resultDto.Request = LinCmsUtils.GetRequest(context.HttpContext);

                    context.Result = new JsonResult(resultDto);

                    if (linLog.Message.IsNullOrEmpty())
                    {
                        linLog.Message = resultDto.Msg?.ToString();
                    }
                }
            }

            linLog.Message += $"{_currentUser.UserName}访问{context.HttpContext.Request.Path},耗时:{Stopwatch.Elapsed.TotalMilliseconds} 毫秒";

            _logService.InsertLog(linLog);

            //记录文本日志
            _logger.LogInformation(JsonConvert.SerializeObject(linLog));

            //MiniProfiler.Current.CustomTiming($"OnActionExecuted ->", str);
        }
コード例 #5
0
 public ResultDto(ErrorCode errorCode, object msg, HttpContext httpContext)
 {
     ErrorCode = errorCode;
     Msg       = msg;
     Request   = LinCmsUtils.GetRequest(httpContext);
 }
コード例 #6
0
 public UnifyResponseDto(ErrorCode errorCode, object message, HttpContext httpContext)
 {
     Code    = errorCode;
     Message = message;
     Request = LinCmsUtils.GetRequest(httpContext);
 }