在某个函数在被调用时用反射记录日志

另参考: http://stackoverflow.com/questions/2405230/can-i-get-parameter-names-values-procedurally-from-the-currently-executing-funct
/// <summary>
        /// 方法反射
        /// </summary>
        /// <param name="mb"></param>
        /// <returns></returns>
        private static string MethodReflect(MethodBase mb)
        {
            string ReflectString = string.Empty;
            ReflectString = mb.Name;
            ReflectString += "(";
            ParameterInfo[] pars = mb.GetParameters();
            foreach (ParameterInfo par in pars)
            {
                if (par.Position != 0)
                {
                    ReflectString += ", ";
                }
                ReflectString += string.Format("{0} {1}", par.ParameterType.Name, par.Name);
            }
            ReflectString += ")";
            return ReflectString;
        }

调用举例:

// 反射
            string ReflectString = MethodReflect(MethodBase.GetCurrentMethod());
日志记录:

 /// <summary>
        /// 日志锁定
        /// </summary>
        private readonly static Object Lok = new Object();

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="txt"></param>
        private static void Log(string Txt)
        {
            try
            {
                lock (Lok)
                {
                    string logDir = System.Configuration.ConfigurationManager.AppSettings["LogDir"];
                    string logPath = string.Format(@"{0}\Login", logDir);
                    string logFile = string.Format(@"{0}\LoginData.{1}.log", logPath, DateTime.Now.ToString(@"yy-MM-dd"));
                    //
                    string logContent = string.Format("{0}\t{1}\r\n", DateTime.Now.ToString(@"HH:mm:ss"), Txt);
                    System.IO.Directory.CreateDirectory(logPath);
                    System.IO.File.AppendAllText(logFile, logContent);
                }
            }
            catch (System.Exception ex)
            {
                ex.ToString();
            }
        }

demo:

/// <summary>
        /// 登录踪迹
        /// </summary>
        /// <param name="onPack"></param>
        /// <param name="toPack"></param>
        /// <returns></returns>
        public static bool LoginTrace(ref LoginOnPack onPack, ref LoginToPack toPack)
        {
            // 反射
            string ReflectString = MethodReflect(MethodBase.GetCurrentMethod());
            // BODY
            try
            {
                // LOG-CALL
                Log(string.Format("CALL --> {0}", ReflectString));
                // CNN
                using (SqlConnection cnn = new SqlConnection())
                {
                    cnn.ConnectionString = ConfigurationManager.ConnectionStrings["Liao"].ToString();
                    cnn.Open();
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        // CMD-TXT
                        cmd.Connection = cnn;
                        cmd.CommandText = "";
                        cmd.CommandText += "INSERT INTO [Login] ([MemberID], [LoginTime], [ReturnFlag], [ReturnText])" + Environment.NewLine;
                        cmd.CommandText += "VALUES (@MemberID, @LoginTime, @ReturnFlag, @ReturnText)";
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@MemberID", toPack.MemberID);
                        cmd.Parameters.AddWithValue("@LoginTime", toPack.LoginTime);
                        cmd.Parameters.AddWithValue("@ReturnFlag", toPack.ReturnFlag);
                        cmd.Parameters.AddWithValue("@ReturnText", toPack.ReturnText);
                        // CMD-LOG
                        Log(SqlToText(cmd));
                        // CMD-EXE
                        cmd.ExecuteNonQuery();
                    }
                }
                // LOG-SUCCESS
                Log(string.Format("SUCCESS --> {0}", ReflectString));
                //
                return true;
            }
            catch (Exception ex)
            {
                // LOG-FAILURE
                Log(string.Format("FAILURE --> {0}\r\n{1}", ReflectString, ex));
                // RETURN
                return false;
            } 
        }


你可能感兴趣的:(反射,日志,MethodBase)