コード例 #1
0
        internal 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 - _sourceCodeLineCount, 1);
            var postErrorLineNumberInFile = errorEndLineNumberInFile + _sourceCodeLineCount;
            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 = codeBlock.Take(errorStartLineNumberInArray).ToArray();
            frame.ContextCode    = codeBlock
                                   .Skip(errorStartLineNumberInArray)
                                   .Take(numOfErrorLines)
                                   .ToArray();
            frame.PostContextCode = codeBlock
                                    .Skip(errorStartLineNumberInArray + numOfErrorLines)
                                    .ToArray();
        }
コード例 #2
0
        internal StackFrameSourceCodeInfo GetStackFrameSourceCodeInfo(string?method, string?filePath, int lineNumber)
        {
            var stackFrame = new StackFrameSourceCodeInfo
            {
                Function = method,
                File     = filePath,
                Line     = lineNumber
            };

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

            IEnumerable <string>?lines = null;

            if (File.Exists(stackFrame.File))
            {
                lines = File.ReadLines(stackFrame.File);
            }
            else
            {
                // Handle relative paths and embedded files
                var fileInfo = _fileProvider.GetFileInfo(stackFrame.File);
                if (fileInfo.Exists)
                {
                    // ReadLines doesn't accept a stream. Use ReadLines as its more efficient
                    // relative to reading lines via stream reader
                    if (!string.IsNullOrEmpty(fileInfo.PhysicalPath))
                    {
                        lines = File.ReadLines(fileInfo.PhysicalPath);
                    }
                    else
                    {
                        lines = ReadLines(fileInfo);
                    }
                }
            }

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

            return(stackFrame);
        }