本文整理汇总了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);
}
}
示例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;
}
示例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 { }
}
示例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);
}
}
示例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());
}
示例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
{
}
}
示例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());
}
示例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{
}
}
示例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));
}
示例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();
}
示例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;
}
}
示例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);
}
示例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();
}
示例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;
}
示例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";
}