当前位置: 首页>>代码示例>>C#>>正文


C# StackTrace.GetFrame方法代码示例

本文整理汇总了C#中System.Diagnostics.StackTrace.GetFrame方法的典型用法代码示例。如果您正苦于以下问题:C# StackTrace.GetFrame方法的具体用法?C# StackTrace.GetFrame怎么用?C# StackTrace.GetFrame使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Diagnostics.StackTrace的用法示例。


在下文中一共展示了StackTrace.GetFrame方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TraceCaller

		public static void TraceCaller(string msg)
		{
			// puts out the callstack and the msg
			try
			{
				StackTrace trace = new StackTrace();
				if (trace != null && trace.GetFrame(1) != null)
				{
					MethodBase method = trace.GetFrame(1).GetMethod();
					string line = ExtractInfo(method) + ": " + msg + "\n" + " caller is:";
					if (trace.GetFrame(2) != null)
					{
						method = trace.GetFrame(2).GetMethod();
						line += ExtractInfo(method);
					}
					LogLine(line);
				}
				else
				{
					LogLine("Method Unknown: " + msg);
				}
			}
			catch (Exception e)
			{
				Console.WriteLine(e.StackTrace);
			}
		}
开发者ID:ViniciusConsultor,项目名称:ecustomsgs1,代码行数:27,代码来源:Tracing.cs

示例2: GetCalledMethod

        /**
         * Retorna o nome do metodo e da classe chamada
         * param frame: Indice do frame na pilha de execucao do qual se deseja extrair o nome do metodo e da classe
         * Obs: o indice 0 representa o metodo atual, 1 representa quem chamou este metodo e assim por diante
         */
        public static void GetCalledMethod(out string MethodName, out string ClassName, int frame = 2)
        {
            StackTrace stackTrace = new StackTrace();

            MethodName = stackTrace.GetFrame(frame).GetMethod().Name;
            ClassName = stackTrace.GetFrame(frame).GetMethod().DeclaringType.Name;
        }
开发者ID:jsabino,项目名称:Arreceba,代码行数:12,代码来源:Util.cs

示例3: Main

        static void Main(string[] args)
        {
            try
            {
                TwitterSearch.TwitterMain();

            }
            catch (Exception ex)
            {
                var st = new StackTrace(ex, true);
                var frame = st.GetFrame(0);
                var line = st.GetFrame(0).GetFileLineNumber();

                MethodBase site = ex.TargetSite;
                string sMethodName = site == null ? null : site.Name;

                Console.WriteLine("------------------");
                Console.WriteLine(DateTime.Now.ToString());
                Console.WriteLine("Line: " + line.ToString());
                Console.WriteLine("Method: " + sMethodName);
                Console.WriteLine("Exception: " + ex.Message);
                Console.Write(ex.StackTrace.ToString());
                Console.WriteLine("");
            }
            catch { }
        }
开发者ID:Blissgig,项目名称:TwitterConsole,代码行数:26,代码来源:Program.cs

示例4: AddData

        //this function is called inside AddData(string testDataPath, string testCase)
        public void AddData(string testDataPath, string testCase)
        {
            try
            {
                testData.GetTestData(testDataPath, testCase);

                AddData(0);
            }

            catch (Exception ex)
            {
                var st = new StackTrace(ex, true);
                var testFrame = st.GetFrame(0);
                for (int i = 0; i < st.FrameCount; i++)
                {
                    testFrame = st.GetFrame(i);
                    if (testFrame.GetFileName() != null)
                    {
                        if (testFrame.GetFileName().ToString().Contains("CUITFramework.cs") == true)
                        {
                            break;
                        }
                    }

                }
                // Get the top stack frame
                var frame = testFrame;
                // Get the line number from the stack frame
                var line = frame.GetFileLineNumber();
                logTofile(_eLogPtah, "Error in AddData and in line: " + line);
                throw new Exception(ex.Message);
            }
        }
开发者ID:prasannarhegde2015,项目名称:DocumentsEJ2012,代码行数:34,代码来源:CUITFramework.cs

示例5: FRIEND

 internal static void FRIEND(string ns)
 {
     StackTrace trace = new StackTrace();
     string text1 = trace.GetFrame(1).GetMethod().DeclaringType.Namespace;
     string str = trace.GetFrame(2).GetMethod().DeclaringType.Namespace;
     Assert(str.Equals(str) || str.Equals(ns), Environment.GetResourceString("RtType.InvalidCaller"), trace.ToString());
 }
开发者ID:randomize,项目名称:VimConfig,代码行数:7,代码来源:ASSERT.cs

示例6: DumpError

        /// <summary>
        /// Writes error to error log file.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="stackTrace"></param>
        public static void DumpError(Exception x,StackTrace stackTrace)
        {
            try
            {
                string source = stackTrace.GetFrame(0).GetMethod().DeclaringType.FullName + "." + stackTrace.GetFrame(0).GetMethod().Name + "()";

                string errorText  = "";
                errorText += "//------------- function:" + source + "  " + DateTime.Now.ToString() + "------------//\r\n";
                errorText += x.Source + ":" + x.Message + "\r\n";
                errorText += x.StackTrace;

                if(x is System.Data.SqlClient.SqlException)
                {
                    System.Data.SqlClient.SqlException sX = (System.Data.SqlClient.SqlException)x;
                    errorText += "\r\n\r\nSql errors:\r\n";

                    foreach(System.Data.SqlClient.SqlError sErr in sX.Errors)
                    {
                        errorText += "\n";
                        errorText += "Procedure: '" + sErr.Procedure + "'  line: " + sErr.LineNumber.ToString() + "  error: " + sErr.Number.ToString() + "\r\n";
                        errorText += "Message: " + sErr.Message + "\r\n";
                    }
                }

                SCore.WriteLog(m_Path + "mailServiceError.log",errorText);
            }
            catch
            {
            }
        }
开发者ID:janemiceli,项目名称:authenticated_mail_server,代码行数:35,代码来源:Error.cs

示例7: ErrorEvent

 // Methods
 public ErrorEvent()
 {
     base.m_Severity = EventSeverity.Error;
       base.m_EventID = 0x3e8;
       base.m_Message = string.Format("Error in '{0}'", base.m_CallingMethod.ToString());
       StringBuilder builder1 = new StringBuilder(0x3e8);
       try
       {
     StackTrace trace1 = new StackTrace(true);
     int num1 = 2;
     Type type1 = null;
     do
     {
       num1++;
       type1 = trace1.GetFrame(num1).GetMethod().DeclaringType;
     } while ((num1 < trace1.FrameCount) && type1.IsSubclassOf(typeof (BaseEvent)));
     for (int num2 = num1; num2 < trace1.FrameCount; num2++)
     {
       StackFrame frame1 = trace1.GetFrame(num2);
       if (num2 > num1)
       {
     builder1.Append('|');
       }
       builder1.AppendFormat("{{{0}}}:{1}:{2}", ReflectionHelper.MethodSignature(frame1.GetMethod()),
                         frame1.GetFileName(), frame1.GetFileLineNumber());
     }
       }
       catch
       {
       }
       base.AddProperty("StackTrace", builder1.ToString());
 }
开发者ID:bmadarasz,项目名称:ndihelpdesk,代码行数:33,代码来源:ErrorEvent.cs

示例8: DumpError

        /// <summary>
        /// Writes error to error log file.
        /// </summary>
        /// <param name="virtualServer">Virtual server name.</param>
        /// <param name="x"></param>
        /// <param name="stackTrace"></param>
        public static void DumpError(string virtualServer,Exception x,StackTrace stackTrace)
        {
            try{
                string source = stackTrace.GetFrame(0).GetMethod().DeclaringType.FullName + "." + stackTrace.GetFrame(0).GetMethod().Name + "()";

                string errorText  = x.Message + "\r\n";
                       errorText += "//------------- function: " + source + "  " + DateTime.Now.ToString() + "------------//\r\n";
                       errorText += x.Source + " : " + x.Message + "\r\n";
                       errorText += x.StackTrace;

                if(x is System.Data.SqlClient.SqlException){
                    System.Data.SqlClient.SqlException sX = (System.Data.SqlClient.SqlException)x;
                    errorText += "\r\n\r\nSql errors:\r\n";

                    foreach(System.Data.SqlClient.SqlError sErr in sX.Errors){
                        errorText += "\n";
                        errorText += "Procedure: '" + sErr.Procedure + "'  line: " + sErr.LineNumber.ToString() + "  error: " + sErr.Number.ToString() + "\r\n";
                        errorText += "Message: " + sErr.Message + "\r\n";
                    }
                }

                if(x.InnerException != null){
                       errorText += "\r\n\r\n//------------- Innner Exception ----------\r\n" + x.Message + "\r\n";
                       errorText += x.InnerException.ToString();
                }

                DumpError(virtualServer,errorText);
            }
            catch{
            }
        }
开发者ID:janemiceli,项目名称:authenticated_mail_server,代码行数:37,代码来源:Error.cs

示例9: Print

 public static void Print(String str)
 {
     var trace = new StackTrace();
     _mainWindow.PrintLine(string.Format("[{0} - {1}.{2}]: {3}", DateTime.Now.ToShortTimeString(),
                                        trace.GetFrame(1).GetMethod().DeclaringType.Name,
                                        trace.GetFrame(1).GetMethod().Name, str));
 }
开发者ID:nuzayets,项目名称:Snowshoes,代码行数:7,代码来源:Snowshoes.cs

示例10: Generate

        /// <summary>
        /// Generates the HTML page with exception information
        /// </summary>
        /// <param name="e">Exception to get information from.</param>
        /// <param name="hideExceptionDetails">if set to <c>true</c> then exception details will not be shown.</param>
        /// <returns></returns>
        public static string Generate(Exception e, bool hideExceptionDetails = false)
        {
            if (e == null)
                return null;

            var tpl = Template.FromManifest("Diagnostics.ExceptionInfoPage.html");

            tpl.Set("AcspNetVersion", new AssemblyInfo(Assembly.GetCallingAssembly()).Version);

            if (hideExceptionDetails)
            {
                tpl.Set("ExceptionDetails", "");
                return tpl.Get();
            }

            var detailsTpl = Template.FromManifest("Diagnostics.ExceptionIDetails.html");

            var trace = new StackTrace(e, true);

            if (trace.FrameCount == 0)
                return null;

            var fileLineNumber = trace.GetFrame(0).GetFileLineNumber();
            var fileColumnNumber = trace.GetFrame(0).GetFileColumnNumber();

            var positionPrefix = fileLineNumber == 0 && fileColumnNumber == 0 ? "" : String.Format("[{0}:{1}]", fileLineNumber, fileColumnNumber);

            detailsTpl.Set("StackTrace", String.Format("{0} {1} : {2}{3}{4}{5}", positionPrefix, e.GetType(),
                e.Message, Environment.NewLine + Environment.NewLine, trace, GetInnerExceptionData(1, e.InnerException)).Replace(Environment.NewLine, "<br />"));

            tpl.Set("ExceptionDetails", detailsTpl);

            return tpl.Get();
        }
开发者ID:jmptrader,项目名称:AcspNet,代码行数:40,代码来源:ExceptionInfoPageGenerator.cs

示例11: FirstNonWrappedMethod

        /// <summary>
        /// Walk through stack until you encounter the first class that is not to be ignored and
        /// whose method does not have the <>
        /// </summary>
        /// <param name="typeToIgnore">Type to ignore as you travel the stack</param>
        /// <returns></returns>
        public static MethodBase FirstNonWrappedMethod(Type typeToIgnore)
        {
            // Go at least one up to ignore this method.
            int index = 1;
            StackTrace st = new StackTrace();

            MethodBase mb = st.GetFrame(index).GetMethod();
            while (true) {

                if ((typeToIgnore == null) || mb.DeclaringType.Name != typeToIgnore.Name && !mb.Name.Contains("<")) {
                    return mb;
                }

                ++index;
                StackFrame sf = st.GetFrame(index);
                if (sf == null) {
                    return mb;
                }

                MethodBase tmp = sf.GetMethod();
                if (tmp == null) {
                    return mb;
                }
                mb = tmp;
            }
        }
开发者ID:MichaelRoop,项目名称:NET_TestHarness,代码行数:32,代码来源:StackFrameTools.cs

示例12: Debug

 /**
  * 向日志文件写入调试信息
  * @param className 类名
  * @param content 写入内容
  */
 public static void Debug(string message) {
     StackTrace st = new StackTrace(true);
     String className = st.GetFrame(1).GetMethod().DeclaringType.FullName;
     String methodName = st.GetFrame(1).GetMethod().Name;
     Debug(className, methodName, message);
 
 }
开发者ID:FuChuan7,项目名称:WeiXin-ASP.NET-MVC3,代码行数:12,代码来源:Log.cs

示例13: Generate

		/// <summary>
		/// Generates the HTML page with exception information
		/// </summary>
		/// <param name="e">Exception to get information from.</param>
		/// <param name="hideExceptionDetails">if set to <c>true</c> then exception details will not be shown.</param>
		/// <returns></returns>
		public static string Generate(Exception e, bool hideExceptionDetails = false)
		{
			if (e == null)
				return null;

			var tpl = Template.FromManifest("Diagnostics.ExceptionInfoPage.html");

			tpl.Set("Simplify.Web.Version", new AssemblyInfo(Assembly.GetCallingAssembly()).Version);

			if (hideExceptionDetails)
			{
				tpl.Set("ExceptionDetails", "");
				return tpl.Get();
			}

			var detailsTpl = Template.FromManifest("Diagnostics.ExceptionIDetails.html");

			var trace = new StackTrace(e, true);

			if (trace.FrameCount == 0)
				return null;

			var fileLineNumber = trace.GetFrame(0).GetFileLineNumber();
			var fileColumnNumber = trace.GetFrame(0).GetFileColumnNumber();

			var positionPrefix = fileLineNumber == 0 && fileColumnNumber == 0 ? "" : $"[{fileLineNumber}:{fileColumnNumber}]";

			detailsTpl.Set("StackTrace",
				$"<b>{positionPrefix} {e.GetType()} : {e.Message}</b>{Environment.NewLine}{trace}{GetInnerExceptionData(1, e.InnerException)}"
					.Replace(Environment.NewLine, "<br />"));

			tpl.Set("ExceptionDetails", detailsTpl);

			return tpl.Get();
		}
开发者ID:i4004,项目名称:Simplify.Web,代码行数:41,代码来源:ExceptionInfoPageGenerator.cs

示例14: FindFrame

        // find first frame that is not "Trace"
        /// <summary>
        /// Searches through the stack trace, and locates the first method in the frames NOT matching the <see cref="Log"/> methods
        /// </summary>
        /// <param name="caller">
        /// A <see cref="System.String"/> which upon success will contain the name of the caller
        /// </param>
        /// <param name="callerClass">
        /// A <see cref="System.String"/> which upon success will contain the class name of the calling method
        /// </param>
        /// <param name="callerName">
        /// A <see cref="System.String"/> which upon success will contain the full assembly name of the caller.
        /// </param>
        /// <param name="fullName">
        /// A <see cref="System.String"/> which upon success will contain the fully qualified name of the caller
        /// </param>
        /// <returns>
        /// A <see cref="System.Boolean"/> which is <c>true</c> if a match was found, <c>false</c> otherwise
        /// </returns>
        public static bool FindFrame(out string caller, out string callerClass, out string callerName, out string fullName, out string callerNamespace)
        {
            string ignoredCallers = "Trace,Debug,Information,Warning,Error,Fatal,SetCallerInContext,ToLog";

            StackTrace stackTrace = new StackTrace();

            for (int i=1; i<=stackTrace.FrameCount; i++)
            {
                caller = stackTrace.GetFrame(i).GetMethod().Name;
                callerClass = stackTrace.GetFrame(i).GetMethod().DeclaringType.Name;
                callerName = stackTrace.GetFrame(i).GetMethod().DeclaringType.Assembly.GetName().Name;
                fullName = stackTrace.GetFrame(i).GetMethod().DeclaringType.FullName;
                callerNamespace = stackTrace.GetFrame(i).GetMethod().DeclaringType.Namespace;

                if (!ignoredCallers.Contains(caller))
                {
                    return true;
                }

            }

            caller = String.Empty;
            callerClass = String.Empty;
            callerName = String.Empty;
            fullName = String.Empty;
            callerNamespace = String.Empty;
            return false;
        }
开发者ID:isharpsolutions,项目名称:Missing,代码行数:47,代码来源:LogTools.cs

示例15: GetPosition

        private static string GetPosition()
        {
            var st = new StackTrace(new StackFrame(2, true));

            var currentFile = st.GetFrame(0).GetFileName();
            var currentLine = st.GetFrame(0).GetFileLineNumber();
            return "File:" + currentFile + " Line:" + currentLine + "\t\n";
        }
开发者ID:bperreault,项目名称:autox,代码行数:8,代码来源:Log.cs


注:本文中的System.Diagnostics.StackTrace.GetFrame方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。