public override void OnActionExecuted(ActionExecutedContext context)
 {
     try
     {
         base.OnActionExecuted(context);
         //请求IP
         var ip       = context.HttpContext.Request.Host.Host;
         var result   = context.Result;
         var clientIp = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
         var index    = clientIp.LastIndexOf(":");
         if (index >= 0)
         {
             clientIp = clientIp.Substring(index + 1);
             if (clientIp == "1")
             {
                 clientIp = "127.0.0.1";
             }
         }
         var log = new Sys_OperateLog()
         {
             ServerIp    = ip,
             Id          = Guid.NewGuid(),
             Url         = $"{context.HttpContext.Request.Scheme}://{ip}{context.HttpContext.Request.Path.Value}",
             ModifyTime  = DateTime.Now,
             RequestType = context.HttpContext.Request.Method,
             ClientIp    = clientIp
         };
         if (typeof(Controller).IsAssignableFrom(context.Controller.GetType()))
         {
             var controller = (Controller)context.Controller;
             log.ControllerName = controller.ControllerContext.ActionDescriptor.ControllerName;
             log.ActionName     = controller.ControllerContext.ActionDescriptor.ActionName;
             //获取方法描述
             var key = $"{log.ControllerName}_{log.ActionName}";
             var dic = _cacheHelp.GetActionDictionary();
             if (dic.ContainsKey(key))
             {
                 log.ActionMemo = dic[key];
             }
         }
         var    resultType    = result.GetType();
         object operateResult = resultType.GetProperty("Value").GetValue(result, null);
         //操作响应结果
         if (operateResult != null)
         {
             var    otype = operateResult.GetType();
             object code  = otype.GetProperty("Code").GetValue(operateResult, null);
             if (code != null)
             {
                 log.ResponseCode = (ResultCode)code;
             }
             object message = otype.GetProperty("Message").GetValue(operateResult, null);
             if (message != null)
             {
                 log.ResponseMessage = message.ToString();
                 if (log.ResponseMessage.Length > 255)
                 {
                     log.ResponseMessage = log.ResponseMessage.Substring(0, 240);
                 }
             }
             object content = otype.GetProperty("Content").GetValue(operateResult, null);
             if (content != null)
             {
                 log.Data = Newtonsoft.Json.JsonConvert.SerializeObject(content);
                 if (log.Data.Length > 1000)
                 {
                     log.Data = log.Data.Substring(0, 990);
                 }
             }
             //用户ID
             var  userIdstr = context.HttpContext.User.Identity.Name;
             Guid userid;
             if (Guid.TryParse(userIdstr, out userid))
             {
                 log.UserId = userid;
             }
             //写入数据库
             _logService.Add(log);
         }
     }
     catch (Exception ex)
     {
         NLogHelp.ErrorLog(ex);
     }
 }
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            try
            {
                base.OnActionExecuted(context);
                var str = context.ToString();

                //请求IP
                var ip     = context.HttpContext.Request.Host.Host;
                var result = context.Result;
                var log    = new Sys_OperateLog()
                {
                    ServerIp   = ip,
                    Id         = Guid.NewGuid(),
                    Url        = $"{context.HttpContext.Request.Scheme}://{ip}{context.HttpContext.Request.Path.Value}",
                    ModifyTime = DateTime.Now,
                    ActionMemo = _action
                };
                if (typeof(Controller).IsAssignableFrom(context.Controller.GetType()))
                {
                    var controller = (Controller)context.Controller;
                    log.ControllerName = controller.ControllerContext.ActionDescriptor.ControllerName;
                    log.RequestType    = controller.ControllerContext.ActionDescriptor.ActionName;
                }
                var    resultType    = result.GetType();
                object operateResult = resultType.GetProperty("Value").GetValue(result, null);
                //操作响应结果
                if (operateResult != null)
                {
                    var    otype = operateResult.GetType();
                    object code  = otype.GetProperty("Code").GetValue(operateResult, null);
                    if (code != null)
                    {
                        log.ResponseCode = (ResultCode)code;
                    }
                    object message = otype.GetProperty("Message").GetValue(operateResult, null);
                    if (message != null)
                    {
                        log.ResponseMessage = message.ToString();
                        if (log.ResponseMessage.Length > 255)
                        {
                            log.ResponseMessage = log.ResponseMessage.Substring(0, 240);
                        }
                    }
                    object content = otype.GetProperty("Content").GetValue(operateResult, null);
                    if (content != null)
                    {
                        log.Data = Newtonsoft.Json.JsonConvert.SerializeObject(content);
                        if (log.Data.Length > 1000)
                        {
                            log.Data = log.Data.Substring(0, 990);
                        }
                    }
                    //用户ID
                    var  userIdstr = context.HttpContext.User.Identity.Name;
                    Guid userid;
                    if (Guid.TryParse(userIdstr, out userid))
                    {
                        log.UserId = userid;
                    }
                    //写入数据库
                    //LogManager.WriteLog(log);
                }
            }
            catch (Exception ex)
            {
                NLogHelp.ErrorLog(ex);
            }
        }
Пример #3
0
 public OperateResult <Sys_OperateLog> Add(Sys_OperateLog log)
 {
     _unitOfWork.GetRepository <Sys_OperateLog>().Insert(log);
     _unitOfWork.SaveChanges();
     return(OperateResult <Sys_OperateLog> .Ok("成功", log));
 }