示例#1
0
  /// <summary>
 /// 异常捕获日志
 /// </summary>
 /// <param name="ex">异常对象(必须为属性类)</param>
 /// <param name="rmark">操作简要描述</param>
 public static void LogWriter(Exception ex, string rmark="")
 {
     try
     {
         string logstr = "\r\n-----------------start----------------------\r\n";
         MethodBase m = new StackTrace().GetFrame(1).GetMethod();
         ParameterInfo[] pm = m.GetParameters();
         string classname = m.DeclaringType.ToString();
         string propertyName = m.Name;
         logstr = logstr + "备注:" + rmark + "\r\n";
         //写类名
         logstr = logstr + "函数类名:" + classname + "\r\n";
         //写函数方法
         logstr = logstr + "函数名称为:" + propertyName + "\r\n";
         for (int i = 0; i < pm.Length; i++)
         {
             logstr = logstr + "函数的参数有:" + pm[i].Name.ToString() + "\r\n";
         }
         logstr = logstr + "函数异常:" + ex.ToString() + "\r\n";
         logstr += "--------------------end---------------------\r\n";
         //写日志
         logger.Info(logstr);
     }
     catch (Exception)
     {
         throw;
     }
 }
示例#2
0
 /// <summary>
 /// 备注日志
 /// </summary>
 /// <param name="dec">需要捕获的参数(必须为属性类)</param>
 /// <param name="rmark">描述操作</param>
 public static void LogWriter(string rmark = "", object dec = null)
 {
     try
     {
         string logstr = "\r\n-----------------start----------------------\r\n";
         MethodBase m = new StackTrace().GetFrame(1).GetMethod();
         ParameterInfo[] pm = m.GetParameters();
         string classname = m.DeclaringType.ToString();
         string propertyName = m.Name;
         logstr = logstr + "备注:" + rmark + "\r\n";
         //写类名
         logstr = logstr + "函数类名:" + classname + "\r\n";
         //写函数方法
         logstr = logstr + "函数名称为:" + propertyName + "\r\n";
         for (int i = 0; i < pm.Length; i++)
         {
             logstr = logstr + "函数的参数有:" + pm[i].Name.ToString() + "\r\n";
         }
         JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
         logstr = logstr + "函数参数值:" + jsonSerializer.Serialize(dec) + "\r\n";
         logstr += "--------------------end---------------------\r\n";
         //写日志
         logger.Info(logstr);
     }
     catch (Exception)
     {
         throw;
     }
 }
示例#3
0
文件: Util.cs 项目: gatekeep/VARCem
        /// <summary>
        /// Writes the exception stack trace to the console/trace log
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="throwable">Exception to obtain information from</param>
        /// <param name="reThrow"></param>
        public static void StackTrace(string msg, Exception throwable, bool reThrow = true)
        {
            MethodBase mb = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod();

            ParameterInfo[] param      = mb.GetParameters();
            string          funcParams = string.Empty;

            for (int i = 0; i < param.Length; i++)
            {
                if (i < param.Length - 1)
                {
                    funcParams += param[i].ParameterType.Name + ", ";
                }
                else
                {
                    funcParams += param[i].ParameterType.Name;
                }
            }

            Exception inner = throwable.InnerException;

            Console.WriteLine("caught an unrecoverable exception! " + msg);
            Console.WriteLine("---- TRACE SNIP ----");
            Console.WriteLine(throwable.Message + (inner != null ? " (Inner: " + inner.Message + ")" : ""));
            Console.WriteLine(throwable.GetType().ToString());

            Console.WriteLine("<" + mb.ReflectedType.Name + "::" + mb.Name + "(" + funcParams + ")>");
            Console.WriteLine(throwable.Source);
            foreach (string str in throwable.StackTrace.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
            {
                Console.WriteLine(str);
            }
            if (inner != null)
            {
                foreach (string str in throwable.StackTrace.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
                {
                    Console.WriteLine("inner trace: " + str);
                }
            }
            Console.WriteLine("---- TRACE SNIP ----");

            if (reThrow)
            {
                throw throwable;
            }
        }
        public static void LogMethodSignatureAndValues(this Logger logger, params string[] values)
        {
            var method = new StackTrace().GetFrame(1).GetMethod();
            var parameters = method.GetParameters();

            var stringBuilder = new StringBuilder();

            for (int i = 0; i < parameters.Length; i++)
            {
                stringBuilder.Append(String.Format("{0}: {1}", parameters[i].Name, values[i] ?? "Null"));
            }

            logger.Trace("{0}({1})",
                method.Name,
                stringBuilder.ToString()
                );
        }
        public static void LogMethodSignatureTypesAndValues(this Logger logger, params object[] values)
        {
            var method = new StackTrace().GetFrame(1).GetMethod();
            var parameters = method.GetParameters();

            var stringBuilder = new StringBuilder();

            for (int i = 0; i < parameters.Length; i++)
            {
                stringBuilder.Append(String.Format("{0} {1}: {2}, ", parameters[i].ParameterType.Name, parameters[i].Name, values[i] ?? "Null"));
            }

            stringBuilder.Length = stringBuilder.Length - 2; // Remove the last ", "

            logger.Trace("{0}({1})",
                method.Name,
                stringBuilder.ToString()
                );
        }
        /// <summary>
        /// 呼び出し元のメソッド・プロパティ・インデクサ・コンストラクタのパラメーターが NULL かどうかを検証します。
        /// </summary>
        /// <param name="arguments">呼び出し元のパラメーター</param>
        /// <remarks>
        /// <![CDATA[
        /// 呼び出し元メソッド・プロパティ・インデクサ・コンストラクタのパラメーターに RequireAttribute の指定がある引数のみ NULL かどうかを確認します。
        /// 呼び出し元メソッド・プロパティ・インデクサ・コンストラクタのパラメーターの順序、数は一致するように指定してください。
        /// ]]>
        /// </remarks>
        /// <example>
        /// [Require]
        /// public void Hoge(string arg1, string arg2, string arg3)
        /// {
        ///     //throw new ArgumentNullException("arg1");
        ///     RequireAttribute.Validate(arg1, arg2, arg3);
        /// }
        /// public void Hoge([Require]string arg1, string arg2, [Require]string arg3)
        /// {
        ///     //throw new ArgumentNullException("arg1");
        ///     RequireAttribute.Validate(arg1, arg2, arg3);
        /// }
        /// [Require]
        /// public string Hoge
        /// {
        ///     get
        ///     {
        ///         return this._hoge;
        ///     }
        ///     set
        ///     {
        ///         //throw new ArgumentNullException("value");
        ///         RequireAttribute.Validate(value); 
        ///     }
        /// }
        /// [Require]
        /// public int this[int index]
        /// {
        ///     get
        ///     {
        ///         return this._hoge[index];
        ///     }
        ///     set
        ///     {
        ///         //throw new ArgumentNullException("index");
        ///         RequireAttribute.Validate(index, value); 
        ///     }
        /// }
        /// public class Hoge
        /// {
        ///     [Require]
        ///     public Hoge(string arg1, string arg2, string arg3)
        ///     {
        ///         //throw new ArgumentNullException("arg1");
        ///         RequireAttribute.Validate(index, value); 
        ///     }
        /// }
        /// </example>
        /// <exception cref="System.ArgumentException">引数の数が一致しません。</exception>
        public static void Validate(params object[] arguments)
        {
            var call = new StackTrace()
                .GetFrame(1)
                .GetMethod();

            var parameters = call.GetParameters();

            if (parameters.Length != arguments.Length)
            {
                throw new ArgumentException("引数の数が一致しません。");
            }

            var attributeType = typeof(RequireAttribute);

            var isMethodAllCheck = call.IsSpecialName;
            if (isMethodAllCheck == false)
            {
                isMethodAllCheck = call.GetCustomAttributes(attributeType, true).Any();
            }

            for (var i = 0; i < parameters.Length; i++)
            {
                var parameter = parameters[i];

                if (isMethodAllCheck == false && parameter.GetCustomAttributes(attributeType, true).Any() == false)
                {
                    continue;
                }

                if (arguments.ElementAtOrDefault(i) != null)
                {
                    continue;
                }

                throw new ArgumentNullException(parameters[i].Name);
            }
        }