/// <summary> /// Creates a SARIF StackFrame instance from a .NET StackFrame instance /// </summary> /// <param name="stackTrace"></param> /// <returns></returns> public static StackFrame Create(System.Diagnostics.StackFrame dotNetStackFrame) { // This value is -1 if not present int ilOffset = dotNetStackFrame.GetILOffset(); string fileName = dotNetStackFrame.GetFileName(); int nativeOffset = dotNetStackFrame.GetNativeOffset(); MethodBase methodBase = dotNetStackFrame.GetMethod(); Assembly assembly = methodBase?.DeclaringType.Assembly; string fullyQualifiedName = CreateFullyQualifiedName(methodBase); StackFrame stackFrame = new StackFrame { Module = assembly?.GetName().Name, FullyQualifiedLogicalName = fullyQualifiedName }; if (fileName != null) { stackFrame.Uri = new Uri(fileName); stackFrame.Line = dotNetStackFrame.GetFileLineNumber(); stackFrame.Column = dotNetStackFrame.GetFileColumnNumber(); } if (ilOffset != -1) { stackFrame.Offset = ilOffset; } if (nativeOffset != -1) { stackFrame.SetProperty("NativeOffset", nativeOffset.ToString(CultureInfo.InvariantCulture)); } return stackFrame; }
public StackFrame(System.Diagnostics.StackFrame frame) : this() { Method = frame.GetMethod().ToString(); MethodType = frame.GetMethod().DeclaringType.ToString(); FileName = frame.GetFileName(); FileLine = frame.GetFileLineNumber(); FileColumn = frame.GetFileColumnNumber(); }
private static string TryGetFileName(System.Diagnostics.StackFrame stackFrame) { if (!SupportFileNameRetrieval) { return null; } // getting a file name may fail due to security limitations // so if we fail once we fail again in this app domain try { return stackFrame.GetFileName(); } catch (NotSupportedException) { SupportFileNameRetrieval = false; return null; } catch (SecurityException) { SupportFileNameRetrieval = false; return null; } }
/// <summary> This is the inner-most implementation method for the Assert helper class. It implements all of the assertType specific behavior for all assertions that get triggered.</summary> private static void AssertCommon(string mesg, AssertType assertType, System.Diagnostics.StackFrame sourceFrame) { // always log all triggered asserts to the BasicFallbackLog string logStr = Fcns.CheckedFormat("{0} at file:'{1}', line:{2}", mesg, sourceFrame.GetFileName(), sourceFrame.GetFileLineNumber()); if (assertType != AssertType.Log) Logging.BasicFallbackLogging.LogError(logStr); bool ignoreFault = false; // intended to be used by debug user to ignore such asserts on a case by case basis if (assertType == AssertType.Log) { if (assertLogger == null) // in an MT world we might create a few of these simultaneously. This is not a problem as the distribution engine supports such a construct so locking is not required here in order to get valid behavior. assertLogger = new Logging.Logger("MosaicLib.Utils.Assert"); assertLogger.Warning.Emit(mesg); return; } else if (assertType == AssertType.LogFallback) { return; // already done } else if (assertType == AssertType.ThrowException) { if (!ignoreFault) throw new AssertException(mesg, sourceFrame); return; } if (!ignoreFault) { // the remaining types always trigger a breakpoint if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break(); // finally if the type is a FatalExit then call the Kernel32.dll FatalExit entry point if (assertType == AssertType.FatalExit) FatalExit(-1); } }
/// <summary> /// Locates this log entry from a stack frame /// </summary> /// <param name="frame">Location</param> /// <returns>Returns this</returns> public Entry Locate( System.Diagnostics.StackFrame frame ) { return Locate( frame.GetFileName( ), frame.GetFileLineNumber( ), frame.GetFileColumnNumber( ), frame.GetMethod( ).Name ); }
// Instead of visiting each field of stackFrame, // the StackFrame.ToString() method could be used, // but the returned text would not include the class name. private static String MethodCallLog(System.Diagnostics.StackFrame methodCall) { var methodCallLog = new System.Text.StringBuilder(); var method = methodCall.GetMethod(); if (method.DeclaringType == null) return "Method.DeclaringType == null"; methodCallLog.Append(method.DeclaringType.ToString()); methodCallLog.Append("."); methodCallLog.Append(methodCall.GetMethod().Name); var methodParameters = method.GetParameters(); methodCallLog.Append("("); for (Int32 x = 0; x < methodParameters.Length; ++x) { if (x > 0) methodCallLog.Append(", "); var methodParameter = methodParameters[x]; methodCallLog.Append(methodParameter.ParameterType.Name); methodCallLog.Append(" "); methodCallLog.Append(methodParameter.Name); } methodCallLog.Append(")"); var sourceFileName = methodCall.GetFileName(); if (!String.IsNullOrEmpty(sourceFileName)) { methodCallLog.Append(" in "); methodCallLog.Append(sourceFileName); methodCallLog.Append(": line "); methodCallLog.Append(methodCall.GetFileLineNumber().ToString(CultureInfo.InvariantCulture)); } return methodCallLog.ToString(); }
// Instead of visiting each field of stackFrame, the StackFrame.ToString() method could be used, // but the returned text would not include the class name. private static String MethodCallLog(System.Diagnostics.StackFrame p_MethodCall) { System.Text.StringBuilder l_MethodCallLog = new System.Text.StringBuilder(); var l_Method = p_MethodCall.GetMethod(); l_MethodCallLog.Append(l_Method.DeclaringType.ToString()); l_MethodCallLog.Append("."); l_MethodCallLog.Append(p_MethodCall.GetMethod().Name); var l_MethodParameters = l_Method.GetParameters(); l_MethodCallLog.Append("("); for (Int32 x = 0; x < l_MethodParameters.Length; ++x) { if (x > 0) l_MethodCallLog.Append(", "); var l_MethodParameter = l_MethodParameters[x]; l_MethodCallLog.Append(l_MethodParameter.ParameterType.Name); l_MethodCallLog.Append(" "); l_MethodCallLog.Append(l_MethodParameter.Name); } l_MethodCallLog.Append(")"); var l_SourceFileName = p_MethodCall.GetFileName(); if (!String.IsNullOrEmpty(l_SourceFileName)) { l_MethodCallLog.Append(" in "); l_MethodCallLog.Append(l_SourceFileName); l_MethodCallLog.Append(": line "); l_MethodCallLog.Append(p_MethodCall.GetFileLineNumber().ToString()); } return l_MethodCallLog.ToString(); }
public static void log( string s, System.Diagnostics.StackFrame sf ) { String file = sf.GetFileName(); if( file == null ) file = ""; //if( file != null ) //file = file.Substring( file.LastIndexOf( "\\" ) + 1 ); #if !BUILD_ANDROID if( LogToFile ) { lock ( sw ) { sw.Write( ApplicationName ); sw.Write( " : " ) ; if( LogTimeDelta ) { TimeSpan delta = DateTime.Now.Subtract( then ); sw.WriteLine( ( delta.ToString() ) //DateTime.Now.ToString("hh.mm.ss.fff") + "@" + file + "(" + sf.GetFileLineNumber() + "):" + s ); then = DateTime.Now; sw.Flush(); } else { sw.WriteLine( ( DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss.fff" ) ) + "@" + file + "(" + sf.GetFileLineNumber() + "):" + s ); sw.Flush(); } } } if( LogToConsole ) #endif { System.Diagnostics.Debug.WriteLine( //Console.WriteLine( //"{0}{1}" file + "(" + sf.GetFileLineNumber() + "):" + System.Threading.Thread.CurrentThread.ManagedThreadId + "|" + s ); } }