Ejemplo n.º 1
0
        // make it internal to enable unit testing
        internal static void ReadFrameContent(
            StackFrameSourceCodeInfo frame,
            IEnumerable <string> allLines,
            int errorStartLineNumberInFile,
            int errorEndLineNumberInFile)
        {
            // Get the line boundaries in the file to be read and read all these lines at once into an array.
            var preErrorLineNumberInFile  = Math.Max(errorStartLineNumberInFile - 10, 1);
            var postErrorLineNumberInFile = errorEndLineNumberInFile + 10;
            var codeBlock = allLines
                            .Skip(preErrorLineNumberInFile - 1)
                            .Take(postErrorLineNumberInFile - preErrorLineNumberInFile + 1)
                            .ToArray();

            var numOfErrorLines             = errorEndLineNumberInFile - errorStartLineNumberInFile + 1;
            var errorStartLineNumberInArray = errorStartLineNumberInFile - preErrorLineNumberInFile;

            frame.PreContextLine = preErrorLineNumberInFile;
            frame.PreContextCode = string.Join("\n", codeBlock.Take(errorStartLineNumberInArray));
            frame.ContextCode    = string.Join("\n", codeBlock
                                               .Skip(errorStartLineNumberInArray)
                                               .Take(numOfErrorLines));
            frame.PostContextCode = string.Join("\n", codeBlock
                                                .Skip(errorStartLineNumberInArray + numOfErrorLines));
        }
Ejemplo n.º 2
0
        // make it internal to enable unit testing
        internal static StackFrameSourceCodeInfo GetStackFrameSourceCodeInfo(string[] sourcePath, string method,
                                                                             string type, string filePath, int lineNumber)
        {
            var key = $"{method}:{type}:{filePath}:{lineNumber}";

            lock (Cache)
            {
                if (Cache.ContainsKey(key))
                {
                    return(Cache[key]);
                }
            }

            var stackFrame = new StackFrameSourceCodeInfo
            {
                Function = method,
                Type     = type,
                File     = filePath,
                FileName = Path.GetFileName(filePath),
                Line     = lineNumber
            };

            if (string.IsNullOrEmpty(stackFrame.File))
            {
                return(stackFrame);
            }

            IEnumerable <string> lines = null;
            var path = GetPath(sourcePath, filePath);

            if (path != null)
            {
                lines = File.ReadLines(path);
            }

            if (lines != null)
            {
                ReadFrameContent(stackFrame, lines, stackFrame.Line, stackFrame.Line);
            }

            lock (Cache)
            {
                if (!Cache.ContainsKey(key))
                {
                    Cache.Add(key, stackFrame);
                }
            }

            return(stackFrame);
        }