コード例 #1
0
        private void ErrorLogToDataBase(string message, string ip, string path)
        {
            var action = new ActionLogEntity()
            {
                actionDetail = message,
                ipAddress    = ip,
                moduleName   = "异常中间件",
                actionUrl    = path,
                createDate   = DateTime.Now,
                operatorId   = "admin",
                logType      = (int)LogTypeEnum.ERROR
            };

            _actionLogService.AddEntityAsync(action);
        }
コード例 #2
0
        /// <summary>
        /// 日志记录
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var t = context.Controller.GetType();
            //获得方法名
            var actionName = context.RouteData.Values["action"]?.ToString();
            //是否有该特性
            var b = IsThatAttribute <FunctionDescriptionAttribute>(actionName, t);

            if (b.DescriptionValue.Equals(string.Empty))
            {
                return;
            }
            var param = "";

            foreach (var(key, value) in context.ActionArguments)
            {
                param += "\n,\t\t参数描述:" + key + "=" + value;
            }

            if (context.HttpContext.Connection.RemoteIpAddress == null)
            {
                return;
            }
            var ip = context.HttpContext.Connection.RemoteIpAddress.MapToIPv4();

            //日志记录
            Logger.LogInformation(
                $"操作记录:{b.DescriptionValue}\n,\t请求参数:({param})\n\t]\n, \t操作时间:{DateTime.Now}\n, \tIP地址:{ip}");
            if (context.HttpContext.Request.Path.ToString().Contains("/api/action/admin"))
            {
                return;
            }
            var action = new ActionLogEntity()
            {
                actionDetail = b.DescriptionValue,
                ipAddress    = ip.ToString(),
                moduleName   = context.ActionDescriptor.DisplayName,
                actionUrl    = context.HttpContext.Request.Path,
                createDate   = DateTime.Now,
                operatorId   = "admin",
                logType      = (int)LogTypeEnum.INFO
            };

            _actionLogService.AddEntityAsync(action);
        }