Beispiel #1
0
        /// <summary>
        /// 写入sql日志
        /// </summary>
        /// <param name="info">日志对象</param>
        public void WriteLogAsyn(LoggingObject info)
        {
            Task task = new Task(
                () =>
            {
                string sqlString =
                    @"insert into T_API_LOG(LogTime,ClientIP,UserAgent,ClientType,Url,Method,Parameter,ParameterShow,
                                Content,Controller,Action,StatusCode,Duration,UserNo,SessionId,IsVisible)
                          values(@LogTime,@ClientIP,@UserAgent,@ClientType,@Url,@Method,@Parameter,@ParameterShow,
                                @Content,@Controller,@Action,@StatusCode,@Duration,@UserNo,@SessionId,@IsVisible)";
                SqlParameter[] paras =
                {
                    new SqlParameter("@LogTime",       info.LogTime),
                    new SqlParameter("@ClientIP",      info.ClientIp),
                    new SqlParameter("@UserAgent",     info.UserAgent),
                    new SqlParameter("@ClientType",    info.ClientType),
                    new SqlParameter("@Url",           info.Url),
                    new SqlParameter("@Method",        info.Method),
                    new SqlParameter("@Parameter",     info.Parameter),
                    new SqlParameter("@ParameterShow", info.ParameterShow),
                    new SqlParameter("@Content",       info.Content),
                    new SqlParameter("@Controller",    info.Controller),
                    new SqlParameter("@Action",        info.Action),
                    new SqlParameter("@StatusCode",    info.StatusCode),
                    new SqlParameter("@Duration",      info.Duration),
                    new SqlParameter("@UserNo",        info.UserNo),
                    new SqlParameter("@SessionId",     info.SessionId),
                    new SqlParameter("@IsVisible",     info.IsVisible)
                };
                SqlHelper.ExecteNonQuery(System.Data.CommandType.Text, sqlString, paras);
            });

            task.Start();
            task.ContinueWith(
                t =>
            {
                if (t.Exception != null)
                {
                    log.Error("日志记录失败:" + info.ToString(), t.Exception);
                }
            });
        }
Beispiel #2
0
        /// <summary>
        /// 构造日志对象
        /// </summary>
        private LoggingObject BuildLoggingObject(HttpActionExecutedContext actionExecutedContext)
        {
            // 计时
            var    stopWatch = actionExecutedContext.Request.Properties[Key] as Stopwatch;
            double time      = -1;

            if (stopWatch != null)
            {
                stopWatch.Stop();
                time = stopWatch.Elapsed.TotalSeconds;
            }

            // statusCode
            int resposeStatus;

            try
            {
                resposeStatus = (int)actionExecutedContext.Response.StatusCode;
            }
            catch (Exception)
            {
                resposeStatus = (int)HttpStatusCode.InternalServerError;
            }
            // method
            string method = actionExecutedContext.Request.Method.Method;
            // url
            string url = actionExecutedContext.Request.RequestUri.AbsoluteUri;
            // ip
            string ip = actionExecutedContext.Request.GetClientIp();
            // userAgent
            string userAgent = string.Join(";", actionExecutedContext.Request.Headers.UserAgent);
            // clientType
            string clientType = HttpHelper.AnalyzeUserAgent(actionExecutedContext.Request.Headers.UserAgent);
            // controller
            string controllerName = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            // action
            string actionName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName;
            // content isVisible
            var attr =
                actionExecutedContext.ActionContext.ActionDescriptor
                .GetCustomAttributes <LogInfoAttribute>().FirstOrDefault();
            string content;
            bool   isVisible;

            if (attr != null)
            {
                content   = attr.Description;
                isVisible = attr.IsVisible;
            }
            else
            {
                content   = actionName;
                isVisible = false;
            }
            // parameter
            string parameter = string.Empty;
            // parameterShow
            string parameterStr = string.Empty;

            if (method != "GET")
            {
                if (actionExecutedContext.Request.Properties.ContainsKey("ActionParameter"))
                {
                    parameter = actionExecutedContext.Request.Properties["ActionParameter"] as string;
                }

                if (actionExecutedContext.Request.Properties.ContainsKey("ActionParameterShow"))
                {
                    parameterStr = actionExecutedContext.Request.Properties["ActionParameterShow"] as string;
                }
            }
            // userNo sessionId
            int    userNo = -1;
            string token  = string.Empty;

            if (actionExecutedContext.Request.Properties.ContainsKey("AuthorizationInfo"))
            {
                AuthorizationInfo info = actionExecutedContext.Request.Properties["AuthorizationInfo"] as AuthorizationInfo;
                if (info != null)
                {
                    userNo = info.UserId;
                    token  = info.Token;
                }
            }

            var logInfo = new LoggingObject
            {
                LogTime       = DateTime.Now,
                ClientIp      = ip,
                UserAgent     = userAgent,
                ClientType    = clientType,
                Url           = url,
                Method        = method,
                Parameter     = parameter.Length > 4000 ? parameter.Substring(3995) + "..." : parameter,
                ParameterShow = parameterStr.Length > 4000 ? parameterStr.Substring(3995) + "..." : parameterStr,
                Content       = content,
                Controller    = controllerName,
                Action        = actionName,
                StatusCode    = resposeStatus,
                Duration      = Convert.ToDecimal(time),
                UserNo        = userNo,
                SessionId     = token,
                IsVisible     = isVisible
            };

            return(logInfo);
        }