/// <summary> /// Converts an exception stack to a sarif code location list. /// </summary> public static IList<AnnotatedCodeLocation> ToCodeLocations(this Exception exception) { List<AnnotatedCodeLocation> codeLocations = new List<AnnotatedCodeLocation>(); StackTrace stack = new StackTrace(exception); foreach (StackFrame frame in stack.GetFrames()) { AnnotatedCodeLocation codeLocation = new AnnotatedCodeLocation(); MemberInfo member = frame.GetMethod(); if (member != null) { codeLocation.Message = member.ReflectedType.FullName + "." + member.Name; } PhysicalLocationComponent physicalLocation = new PhysicalLocationComponent(); string filename = frame.GetFileName(); if (!String.IsNullOrWhiteSpace(filename)) { physicalLocation.Uri = new Uri(filename); } physicalLocation.Region = new Region(); physicalLocation.Region.StartLine = frame.GetFileLineNumber(); physicalLocation.Region.EndLine = frame.GetFileLineNumber(); physicalLocation.Region.StartColumn = frame.GetFileColumnNumber(); physicalLocation.Region.EndColumn = frame.GetFileColumnNumber(); codeLocation.PhysicalLocation = new List<PhysicalLocationComponent>() { physicalLocation }; codeLocations.Add(codeLocation); } return codeLocations; }
private void PopulateControl(ObservableCollection <AnnotatedCodeLocationModel> items, IList <IList <AnnotatedCodeLocation> > annotatedCodeLocationSets, AnnotatedCodeLocationKind kind) { int index = 0; NewLineIndex newLineIndex = null; Dictionary <string, NewLineIndex> fileToNewLineIndexMap = new Dictionary <string, NewLineIndex>(); foreach (IList <AnnotatedCodeLocation> annotatedCodeLocations in annotatedCodeLocationSets) { index++; // Labels are 1-indexed in UI foreach (AnnotatedCodeLocation codeLocation in annotatedCodeLocations) { PhysicalLocationComponent plc = codeLocation.PhysicalLocation[0]; string fileName = plc.Uri.LocalPath; Region region = plc.Region; if (!File.Exists(fileName)) { fileName = RebaselineFileName(fileName, _remappedPathPrefixes); if (fileName == null) { // User cancelled items.Clear(); return; } } if (!fileToNewLineIndexMap.TryGetValue(fileName, out newLineIndex)) { fileToNewLineIndexMap[fileName] = newLineIndex = new NewLineIndex(File.ReadAllText(fileName)); } region.Populate(newLineIndex); items.Add(new AnnotatedCodeLocationModel() { Index = index, Kind = kind, Message = codeLocation.Message, Region = region, FilePath = fileName, }); } } }