private static void TestSingleEncoding(string text, int bufferSize, Encoding encoding) { DisposeCheckingMemoryStream stream = new DisposeCheckingMemoryStream(encoding.GetBytes(text)); var reader = new ReverseLineReader(() => stream, encoding, bufferSize); AssertLines(new LineReader(() => new StringReader(text)).Reverse(), reader); Assert.IsTrue(stream.Disposed); }
/// <summary> /// Uses a ReverseLineReader to read only the final line of output. /// </summary> /// <param name="reader">Reader.</param> /// <param name="filename">Filename.</param> public override void ReadBody(StreamReader reader, string filename) { ReverseLineReader revReader = new ReverseLineReader(filename); string line = revReader.First(); ReadLine(line); }
public IEnumerable <string> GetLinesSinceLastKnownLine() { if (!string.IsNullOrEmpty(_logFilePath)) { using (Stream stream = File.Open(_logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { if (stream != null) { ReverseLineReader line = new ReverseLineReader(() => stream); // create anonymous method to return Stream as Func<Stream> if (string.IsNullOrEmpty(lastKnownLine)) { var latestLine = line.Take(1).ToList(); lastKnownLine = latestLine.First(); return(latestLine); } var newChatLines = line.Take(15).ToList(); var indexOfLastKnownLine = newChatLines.IndexOf(lastKnownLine); lastKnownLine = newChatLines.First(); if (indexOfLastKnownLine >= 0) { return(newChatLines.Take(indexOfLastKnownLine)); } return(newChatLines); } } } return(new List <string> { "404 -> Logfile not found!" }); }
private void checkForUpdates(string debug = null) { fileTimer.Stop(); var reverseReader = new ReverseLineReader(clientLogPath + "\\client.txt"); var lines = reverseReader.Take(50).ToList(); if (lastLines == null) { lastLines = lines; } else { var newLines = lines.Except(lastLines).ToList(); lastLines = lines; foreach (var line in newLines) { Message message = new Message(line); NewMessage(null, new MessageEventArgs(message)); } } fileTimer.Start(); }
private void exploreXafErrors_Execute(ExecuteEventArgs ea) { Project startUpProject = CodeRush.ApplicationObject.Solution.FindStartUpProject(); Property outPut = startUpProject.ConfigurationManager.ActiveConfiguration.FindProperty(ConfigurationProperty.OutputPath); bool isWeb = IsWeb(startUpProject); string fullPath = startUpProject.FindProperty(ProjectProperty.FullPath).Value + ""; string path = Path.Combine(fullPath, outPut.Value.ToString()) + ""; if (isWeb) path = Path.GetDirectoryName(startUpProject.FullName); Func<Stream> streamSource = () => { var path1 = path + ""; File.Copy(Path.Combine(path1, "expressAppFrameWork.log"), Path.Combine(path1, "expressAppFrameWork.locked"), true); return File.Open(Path.Combine(path1, "expressAppFrameWork.locked"), FileMode.Open, FileAccess.Read, FileShare.Read); }; var reader = new ReverseLineReader(streamSource); var stackTrace = new List<string>(); foreach (var readline in reader) { stackTrace.Add(readline); if (readline.Trim().StartsWith("The error occured:") || readline.Trim().StartsWith("The error occurred:")) { stackTrace.Reverse(); string errorMessage = ""; foreach (string trace in stackTrace) { errorMessage += trace + Environment.NewLine; if (trace.Trim().StartsWith("----------------------------------------------------")) break; } Clipboard.SetText(errorMessage); break; } } }
private static void TestSingleEncoding(string text, int bufferSize, Encoding encoding) { DisposeCheckingMemoryStream stream = new DisposeCheckingMemoryStream(encoding.GetBytes(text)); var reader = new ReverseLineReader(() => stream, encoding, bufferSize); AssertLines(new LineReader(() => new StringReader(text)).Reverse(), reader); Assert.IsTrue(stream.Disposed); }
public void UnwritableStreamIsOkay() { var stream = new DisabledMemoryStream(Encoding.ASCII.GetBytes("foo")) { canWrite = false }; var reader = new ReverseLineReader(() => stream); AssertLines(new[] { "foo" }, reader); }
public void ShouldReturnANewLine() { var contact = new Contact { Name = "Jean", Mail = "*****@*****.**" }; _inventoryCsvManager.AddSerialNumber(contact); var _reverseStream = new ReverseLineReader(_mockedConfig["INVENTORY_PATH"]).Take(2); var inventory_name = _reverseStream.First().Split(',')[2]; Assert.AreEqual(contact.Name, inventory_name); }
public void AddSerialNumber_ShouldAddTheCorrectSerialNumber() { var contact = new Contact { Name = "Jean", Mail = "*****@*****.**" }; _inventoryCsvManager.AddSerialNumber(contact); var _reverseStream = new ReverseLineReader(_mockedConfig["INVENTORY_PATH"]).Take(2); var current = _reverseStream.First().Split(',')[0]; var previous = _reverseStream.Last().Split(',')[0]; Assert.AreEqual(long.Parse(current), long.Parse(previous) + 1); }
private static void AssertInvalidData(ReverseLineReader reader) { try { foreach (string ignored in reader) { } Assert.Fail("Expected exception"); } catch (InvalidDataException) { // Expected } }
public override async Task <List <string> > List(string proj, string logLevel, string keyWrod = null, int page = 1, int pageSize = 100) { page = page <= 1 ? 0 : page; int startLine = page * pageSize; List <string> result = new List <string>(); var fileName = Path.Combine(options.File, proj, logLevel); try { ReverseLineReader reader = new ReverseLineReader(fileName); var doc = reader.GetEnumerator(); int index = 0; while (doc.MoveNext()) { index++; if (index > startLine) { if (!string.IsNullOrEmpty(keyWrod)) { if (doc.Current.Contains(keyWrod)) { result.Add(doc.Current); } } else { result.Add(doc.Current); } } if (result.Count == pageSize) { break; } } } catch (NotSupportedException ex) { logger.LogError(ex.Message, ex); } catch (Exception ex) { logger.LogError(ex.Message, ex); } return(result); }
public void UnseekableStreamThrowsExceptionEagerly() { var stream = new DisabledMemoryStream { canSeek = false }; var reader = new ReverseLineReader(() => stream); try { reader.GetEnumerator(); Assert.Fail("Expected exception"); } catch (NotSupportedException) { // Expected } }
public static void Explore() { Project startUpProject = DteExtensions.DTE.Solution.FindStartUpProject(); Property outPut = startUpProject.ConfigurationManager.ActiveConfiguration.FindProperty(ConfigurationProperty.OutputPath); bool isWeb = startUpProject.IsWeb(); string fullPath = startUpProject.FindProperty(ProjectProperty.FullPath).Value + ""; string path = Path.Combine(fullPath, outPut.Value.ToString()) + ""; if (isWeb) { path = Path.GetDirectoryName(startUpProject.FullName); } Stream StreamSource() { var path1 = path + ""; File.Copy(Path.Combine(path1, "expressAppFrameWork.log"), Path.Combine(path1, "expressAppFrameWork.locked"), true); return(File.Open(Path.Combine(path1, "expressAppFrameWork.locked"), FileMode.Open, FileAccess.Read, FileShare.Read)); } var reader = new ReverseLineReader(StreamSource); var stackTrace = new List <string>(); foreach (var readline in reader) { stackTrace.Add(readline); if (readline.Trim().StartsWith("The error occured:") || readline.Trim().StartsWith("The error occurred:")) { stackTrace.Reverse(); string errorMessage = ""; foreach (string trace in stackTrace) { errorMessage += trace + Environment.NewLine; if (trace.Trim().StartsWith("----------------------------------------------------")) { break; } } Clipboard.SetText(errorMessage); break; } } }
private void InitLastLogLineAsLastKnownLine() { if (!string.IsNullOrEmpty(_logFilePath)) { using (Stream stream = File.Open(_logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { if (stream != null) { ReverseLineReader line = new ReverseLineReader(() => stream); // create anonymous method to return Stream as Func<Stream> if (string.IsNullOrEmpty(lastKnownLine)) { var latestLine = line.Take(1).ToList(); lastKnownLine = latestLine.First(); } } } } }
private static void AssertInvalidData(ReverseLineReader reader) { try { foreach (string ignored in reader) { } Assert.Fail("Expected exception"); } catch (InvalidDataException) { // Expected } }
public void UnwritableStreamIsOkay() { var stream = new DisabledMemoryStream(Encoding.ASCII.GetBytes("foo")) { canWrite = false }; var reader = new ReverseLineReader(() => stream); AssertLines(new[]{"foo"}, reader); }
public void UnseekableStreamThrowsExceptionEagerly() { var stream = new DisabledMemoryStream { canSeek = false }; var reader = new ReverseLineReader(() => stream); try { reader.GetEnumerator(); Assert.Fail("Expected exception"); } catch (NotSupportedException) { // Expected } }