Ejemplo n.º 1
0
        /// <summary>
        /// 根据异常及运行中的相关信息构造完整的异常日志信息
        /// </summary>
        /// <param name="ex">Exception实例(必选)</param>
        /// <param name="context">HttpContext实例(可选)</param>
        /// <param name="dbCommand">DbCommand实例(可选)</param>
        /// <returns></returns>
        public static ExceptionInfo Create(Exception ex, HttpContext context, DbCommand dbCommand)
        {
            if (ex == null)
            {
                throw new ArgumentNullException("ex");
            }

            ExceptionInfo info = new ExceptionInfo();

            info.FillBaseInfo();
            info.Message       = ex.Message;
            info.ExceptionType = ex.GetType().FullName;
            info.Exception     = ex.ToString();

            if (context != null)
            {
                info.HttpInfo = HttpInfo.Create(context);
            }

            if (dbCommand != null)
            {
                info.SqlInfo = SqlInfo.Create(dbCommand);
            }

            return(info);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 根据DbCommand创建并填充SqlInfo对象
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public static SqlInfo Create(DbCommand command)
        {
            if (command == null)
            {
                return(null);
            }

            SqlInfo info = new SqlInfo();

            info.SqlText = command.CommandText.SubstringN(MaxSqlTextLen);

            if (command.Parameters == null || command.Parameters.Count == 0)
            {
                return(info);
            }

            info.Parameters = new List <DbParam>(command.Parameters.Count);

            for (int i = 0; i < command.Parameters.Count; i++)
            {
                if (i < MaxParameterCount)
                {
                    DbParameter parameter = command.Parameters[i];

                    DbParam pv = new DbParam();
                    pv.Name   = parameter.ParameterName;
                    pv.DbType = parameter.DbType.ToString();

                    if (parameter.Value == null || parameter.Value == DBNull.Value)
                    {
                        pv.Value = "NULL";
                    }
                    else
                    {
                        pv.Value = parameter.Value.ToString().SubstringN(MaxParaValueLen);
                    }

                    info.Parameters.Add(pv);
                }
                else
                {
                    DbParam pv = new DbParam();
                    pv.Name   = "#####";
                    pv.DbType = System.Data.DbType.String.ToString();
                    pv.Value  = "参数太多,已被截断...,参数数量:" + command.Parameters.Count.ToString();
                    info.Parameters.Add(pv);
                    break;
                }
            }
            return(info);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 根据DbCommand创建并填充SqlInfo对象
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public static SqlInfo Create(DbCommand command)
        {
            if (command == null)
            {
                return(null);
            }

            SqlInfo info = new SqlInfo();

            info.SqlText = command.CommandText.SubstringN(MaxSqlTextLen);

            if (command.Parameters == null || command.Parameters.Count == 0)
            {
                return(info);
            }

            info.Parameters = new List <NameValue>(command.Parameters.Count);

            for (int i = 0; i < command.Parameters.Count; i++)
            {
                if (i < MaxParameterCount)
                {
                    DbParameter parameter = command.Parameters[i];

                    NameValue nv = new NameValue();
                    nv.Name = parameter.ParameterName;
                    if (parameter.Value != null)
                    {
                        nv.Value = parameter.Value.ToString().SubstringN(MaxParaValueLen);
                    }
                    else
                    {
                        nv.Value = "NULL";
                    }


                    info.Parameters.Add(nv);
                }
                else
                {
                    NameValue nv = new NameValue();
                    nv.Name  = "#####";
                    nv.Value = "参数太多,已被截断...,参数数量:" + command.Parameters.Count.ToString();
                    info.Parameters.Add(nv);
                    break;
                }
            }
            return(info);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 根据异常及运行中的相关信息构造完整的异常日志信息
        /// </summary>
        /// <param name="ex">Exception实例(必选)</param>
        /// <param name="context">HttpContext实例(可选)</param>
        /// <param name="dbCommand">DbCommand实例(可选)</param>
        /// <returns></returns>
        public static ExceptionInfo Create(Exception ex, HttpContext context, DbCommand dbCommand)
        {
            if (ex == null)
            {
                throw new ArgumentNullException("ex");
            }

            ExceptionInfo info = new ExceptionInfo();

            info.FillBaseInfo();
            info.Message       = ex.Message;        // TODO: 应该保证能拿到一个有价值的异常消息
            info.ExceptionType = ex.GetType().FullName;
            info.Exception     = ex.ToString();


            if (ClownFish.Log.Serializer.WriterFactory.Config.LogEnvironmentInfo)
            {
                try {
                    info.Environment = EnvironmentInfo.GetCurrent();
                }
                catch { // 如果获取机器环境信息失败,就直接忽略
                }
            }

            if (context != null)
            {
                info.HttpInfo = HttpInfo.Create(context);
            }

            if (dbCommand != null)
            {
                info.SqlInfo = SqlInfo.Create(dbCommand);
            }

            return(info);
        }