Microsoft.CodeAnalysis.Sarif.Stack.Create C# (CSharp) Method

Create() public static method

Creates a SARIF Stack instance from a .NET StackTrace text representation (as returned by StackTrace.ToString())
public static Create ( string stackTrace ) : Stack
stackTrace string
return Stack
        public static Stack Create(string stackTrace)
        {
            Stack stack = new Stack();

            if (string.IsNullOrEmpty(stackTrace))
            {
                return stack;
            }

            stack.Frames = new List<StackFrame>();

            var regex = new Regex(StackFrame.AT + @"([^)]+\))(" + StackFrame.IN + @"([^:]+:[^:]+)" + StackFrame.LINE + @" (.*))?", RegexOptions.Compiled);

            foreach (string line in stackTrace.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
            {
                // at Type.Method() in File.cs : line X
                string current = line;

                var stackFrame = new StackFrame();

                Match match = regex.Match(line);

                if (match.Success)
                {
                    stackFrame.FullyQualifiedLogicalName = match.Groups[1].Value;

                    if (!string.IsNullOrEmpty(match.Groups[2].Value))
                    {
                        string fileName = match.Groups[3].Value;
                        int lineNumber = int.Parse(match.Groups[4].Value);

                        stackFrame.Uri = new Uri(fileName);
                        stackFrame.Line = lineNumber;
                    }
                }
                stack.Frames.Add(stackFrame);
            }

            return stack;
        }

Usage Example

Esempio n. 1
0
 public static ExceptionData Create(Exception exception)
 {
     return(new ExceptionData
     {
         Kind = exception.GetType().Name,
         Message = exception.Message,
         InnerExceptions = GetInnerExceptions(exception),
         Stack = Stack.Create(exception.StackTrace)
     });
 }