本文整理汇总了C#中System.Diagnostics.StackTrace.GetFrame方法的典型用法代码示例。如果您正苦于以下问题:C# System.Diagnostics.StackTrace.GetFrame方法的具体用法?C# System.Diagnostics.StackTrace.GetFrame怎么用?C# System.Diagnostics.StackTrace.GetFrame使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Diagnostics.StackTrace
的用法示例。
在下文中一共展示了System.Diagnostics.StackTrace.GetFrame方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: append
public static bool append(String message, MessageType mt)
{
try
{
// get name of calling module and function
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
System.Reflection.MethodBase mb = stackTrace.GetFrame(0).GetMethod(); // =this function
String name = mb.Name;
for(int i = 1; i < stackTrace.FrameCount; i++)
{
mb = stackTrace.GetFrame(i).GetMethod();
if(mb.Name != name)
break;
}
//Build and then write the (CSV formatted) line to todays log file
String logName = getLogName();
String dateStamp = DateTime.Today.ToString("yyyyMMdd");
String timeStamp = DateTime.Now.ToString("HH:mm:ss.fff");
String line = String.Format("{0},{1},{2},{3},{4},\"{5}\"{6}", dateStamp, timeStamp, mb.Module, mb.Name, mt.ToString(), message.Replace(',',';').Replace('"','\''), Environment.NewLine);
System.IO.File.AppendAllText(logName, line);
}
catch(Exception ex)
{
// Log errors for testing, but ignore when live
System.Diagnostics.Debug.WriteLine(ex.Message);
return false;
}
return true;
}
示例2: GetStamp
/*
* Gets a stamp for debug messages that contains the time and the calling method.
*
* The calling method is judged to be the lowest frame in the stack that's
* *not* in the FLog class. If depth is set, we will walk further up the
* stack.
*/
public static string GetStamp(int extraDepth=0)
{
string rv = "";
System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace();
// Walk up the stack to the first frame that's *not* in this class (i.e., the lowest caller) and append it.
System.Diagnostics.StackFrame sf = null;
int i = 0;
for ( ; i<trace.FrameCount; i++) {
sf = trace.GetFrame(i);
if (sf.GetMethod().DeclaringType != typeof(FLog))
break;
}
// Walk up some extra frames as needed.
i += extraDepth;
sf = trace.GetFrame(i);
if (sf != null) {
string m = sf.GetMethod().DeclaringType.ToString();
rv += System.String.Format("{0}:{1}.{2}", Time.realtimeSinceStartup.ToString("F1"), m, sf.GetMethod().Name);
} else {
rv = Time.realtimeSinceStartup.ToString("F1");
}
return rv;
}
示例3: Log
public static void Log(string message, bool writeToConsole = true)
{
if (message == null) return;
string assembly = "";
try
{
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
if (stackTrace.FrameCount != 0)
assembly = stackTrace.GetFrame(1).GetMethod().DeclaringType.Assembly.GetName().Name;
if ((assembly.StartsWith("Rocket.") || assembly == "Assembly-CSharp" || assembly == "UnityEngine") && stackTrace.FrameCount > 2)
{
assembly = stackTrace.GetFrame(2).GetMethod().DeclaringType.Assembly.GetName().Name;
}
if (assembly == "" || assembly == typeof(Logger).Assembly.GetName().Name || assembly == lastAssembly || assembly.StartsWith("Rocket.") || assembly == "Assembly-CSharp" || assembly == "UnityEngine")
{
assembly = "";
}
else
{
assembly = assembly + " >> ";
}
lastAssembly = assembly;
message = assembly + message;
}
catch (Exception)
{
throw;
}
ProcessInternalLog(ELogType.Info, message, writeToConsole);
}
示例4: Error
/// <summary>
/// To Log exception informations
/// Stream witer is replaced with enterprise library
/// </summary>
/// <param name="ex"></param>
public static void Error(string message, Exception ex)
{
LogEntry logEntry = null;
try
{
logEntry = new LogEntry();
logEntry.Categories.Clear();
logEntry.TimeStamp = DateTime.Now;
logEntry.Severity = System.Diagnostics.TraceEventType.Error;
logEntry.Message = message;
logEntry.Categories.Add("General Category");
Logger.Write(logEntry);
System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
Logger.Write("Method: " + trace.GetFrame(trace.FrameCount - 1).GetMethod().Name);
Logger.Write("Line: " + trace.GetFrame(trace.FrameCount - 1).GetFileLineNumber());
Logger.Write("Column: " + trace.GetFrame(trace.FrameCount - 1).GetFileColumnNumber());
}
catch
{
throw;
}
finally
{
logEntry = null;
}
}
示例5: DisplayForm
public static void DisplayForm(params string[] testSteps)
{
using (ManualTestForm form = new ManualTestForm())
{
int i = 0;
Console.WriteLine("Steps:");
foreach (string step in testSteps)
{
string message = String.Format("{0} {1}", i, step);
Console.WriteLine(message);
form.TestStepList.Items.Add(message);
++i;
}
System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(1);
form.Text = trace.GetFrame(0).GetMethod().DeclaringType.FullName
+ trace.GetFrame(0).GetMethod().Name;
DialogResult result = form.ShowDialog();
// dumping comments
Console.WriteLine("Manual Test");
Console.WriteLine(form.Comments);
Console.WriteLine("Success: {0}", result);
Assert.AreEqual(result, DialogResult.Yes,
"Manual Test failed");
}
}
示例6: SaveFromADO
public static void SaveFromADO(LogPerformance logPerformance)
{
var st = new System.Diagnostics.StackTrace();
if (String.IsNullOrEmpty(logPerformance.Accion))
logPerformance.Accion = st.GetFrame(1).GetMethod().Name;
if (String.IsNullOrEmpty(logPerformance.Modulo))
logPerformance.Modulo = st.GetFrame(1).GetMethod().ReflectedType.FullName;
Save(logPerformance);
}
示例7: GetLastNonErrorManagerCodeLocation
/// <summary>
/// Return first non ErrorManager code location for generating messages
/// </summary>
/// <param name="e">Current exception</param>
/// <returns></returns>
private static StackFrame GetLastNonErrorManagerCodeLocation(Exception e)
{
StackTrace stackTrace = new StackTrace(e);
int i = 0;
for (; i < stackTrace.FrameCount; i++)
{
StackFrame f = stackTrace.GetFrame(i);
if (f.ToString().IndexOf("ErrorManager") < 0)
{
break;
}
}
StackFrame location = stackTrace.GetFrame(i);
return location;
}
示例8: FMEJobManager
public FMEJobManager()
{
try
{
// Let the User know that the FME Session is being established.
if (ProcessMessage != null)
{
ProcessMessage(" - Opening the FME Session in which the specified FME Job will be run...");
}
// Attempt to instantiate the FME Session.
// Initiate an FME Session.
_fmeSession = Safe.FMEObjects.FMEObjects.CreateSession();
_fmeSession.Init(null);
// Exit this method.
return;
}
catch (Safe.FMEObjects.FMEOException fmeException)
{
// Determine the Line Number from which the exception was thrown.
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(fmeException, true);
System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
int lineNumber = stackFrame.GetFileLineNumber();
// Let the User know that the Session Could not be created.
if (ErrorMessage != null)
{
ErrorMessage("");
ErrorMessage("");
ErrorMessage("Failed to open the FME Session with error - " + fmeException.FmeErrorMessage + " (Line: " + lineNumber.ToString() + ")!");
}
// Exit this method.
return;
}
catch (System.Exception caught)
{
// Determine the Line Number from which the exception was thrown.
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(caught, true);
System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
int lineNumber = stackFrame.GetFileLineNumber();
// Let the User know that the Session Could not be created.
if (ErrorMessage != null)
{
ErrorMessage("");
ErrorMessage("");
ErrorMessage("Failed to open the FME Session with error - " + caught.Message + " (Line: " + lineNumber.ToString() + ")!");
}
// Exit this method.
return;
}
}
示例9: ForceDebugPrint
public static void ForceDebugPrint(String format, params object[] args)
{
System.Diagnostics.StackTrace stack = new System.Diagnostics.StackTrace();
#if DEBUG
System.Diagnostics.Debug.Print("{0,2}:{1,2}:{2,2}.{3,-10}{4,-30}{5}",
DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond,
stack.GetFrame(1).GetMethod().Name,
String.Format(format, args));
#endif
log.WriteLine("{0,2}:{1,2}:{2,2}.{3,-10}{4,-30}{5}",
DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond,
stack.GetFrame(1).GetMethod().Name,
String.Format(format, args));
log.Flush();
}
示例10: Write
public static void Write(string format, params object[] args)
{
#if TRACE
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(1);
System.Reflection.MethodBase methodBase = stackFrame.GetMethod();
Console.WriteLine("[thread " + System.Threading.Thread.CurrentThread.ManagedThreadId + ", " + methodBase.DeclaringType.Name + "." + methodBase.Name + "] " + string.Format(format, args));
#endif
}
示例11: Debug
public void Debug(String v)
{
if (LEVEL == L_DEBUG)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
string message = "[" + DateUtil.GetNowDate() + "][" + this.tag + "][" + st.GetFrame(0).GetFileLineNumber() + "] - " + v;
this.write(message);
}
}
示例12: GetMethodName
private static string GetMethodName(int depth)
{
var stackTrace = new System.Diagnostics.StackTrace();
var stackFrame = stackTrace.GetFrame(depth);
var methodBase = stackFrame.GetMethod();
string n = methodBase.DeclaringType.Name + "." + methodBase.Name;
return n;
}
示例13: LogMethod
public static void LogMethod(params object[] toLog)
{
System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace();
System.Diagnostics.StackFrame callerFrame = trace.GetFrame(1);
MethodBase method = callerFrame.GetMethod();
indent++;
Debug.Log(string.Format("{0}.{1}\n{2}", method.DeclaringType, method.Name, LogToString(toLog)));
indent--;
}
示例14: Check
public static void Check(bool express)
{
if (express)
return;
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
System.Diagnostics.StackFrame sf = st.GetFrame(0);
string msg = "Assert Excpetion, file:" + sf.GetFileName() + "method:" + sf.GetMethod().Name + "line:" + sf.GetFileLineNumber();
throw new Exception(msg);
}
示例15: Log
internal void Log(params object[] parameters)
{
var st = new System.Diagnostics.StackTrace();
var method = st.GetFrame(1).GetMethod();
Log(method.Name, method.GetParameters().Select((param, index) => {
var tmp = param.ToString().Split(' ');
return string.Format("{0} {1} = {2}", GetName(tmp.First()), tmp.Last(), parameters[index]);
}).ToArray());
}