Ejemplo n.º 1
0
        public static void Error(string errorMessage, string stackTrace, params string[] param)
        {
            try
            {
                MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod();

                string actionName = methodBase.Name;
                string controllerName = methodBase.DeclaringType.Name;

                HttpPostAttribute authorizationAttributes = methodBase.GetCustomAttributes(typeof(HttpPostAttribute), false)
                  .Cast<HttpPostAttribute>().FirstOrDefault();

                if (authorizationAttributes != null)
                {
                    actionName += "[POST]";
                }

                string logMessage = string.Concat(controllerName, "|", actionName, "|", errorMessage, "|", stackTrace, "|", param);

                ErrorLogger.Error(logMessage);
            }
            catch (Exception e)
            {

                ErrorLogger.Error("Unable to log.Error: " + e.Message + ",Stack Trace:" + e.StackTrace);
            }
        }
        /// <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);
            }
        }
Ejemplo n.º 3
0
        public static void Info(string remoteIp, params string[] param)
        {
            try
            {
                MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod();
                string actionName = methodBase.Name;
                string controllerName = methodBase.DeclaringType.Name;

                HttpPostAttribute authorizationAttributes = methodBase.GetCustomAttributes(typeof(HttpPostAttribute), false)
                    .Cast<HttpPostAttribute>().FirstOrDefault();

                if (authorizationAttributes != null)
                {
                    actionName += "[POST]";
                }

                string paramValues = null;
                for (int i = 0; i < param.Length; i++)
                {
                    paramValues += string.Concat(" | " + param[i]);
                }

                string logMessage = string.Concat(controllerName, "|", actionName, "|", paramValues);

                InfoLogger.Info(logMessage);
            }
            catch (Exception e)
            {
                InfoLogger.Error("Unable to log.Error: " + e.Message + ",Stack Trace:" + e.StackTrace);
            }
        }