0

I am trying to build a little tool for our messaging (WCF) component. The idea is to give responses based on previous trace logs for testing purposes. As the SOAP messages are very big we tend to have huge trace files. What i want to do is read the traces from end to start line by line (newest to oldest) in order to build my responses. Anyone has any idea how i can do that in .NET?. It seems that FileStream class supports only forward reading.

Adrian Zanescu
  • 7,907
  • 6
  • 35
  • 53
  • See also: [Get last 10 lines of very large text file > 10GB c#](http://stackoverflow.com/questions/398378), [How to read a text file reversely with iterator in C#](http://stackoverflow.com/questions/452902) – hippietrail Nov 05 '12 at 19:14

1 Answers1

2

If these are text files, I have a ReverseLineReader (or some such) in MiscUtil which you may find useful. It only supports certain encodings (Unicode, UTF-8 or any fixed-single-byte encoding) but hopefully that'll be enough for you.

It returns the strings via an iterator, so you can use LINQ to limit how many you read etc, and they're read lazily.

I assume you don't actually want to read the whole file? If you do, I'd suggest using File.ReadAllLines and then reversing the resultant array :)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • thanks. I'll take a look at it. But the idea was to get guidance of how one could implement it on it's own and not a link to an already done solution :). (for learning purposes) – Adrian Zanescu Jan 08 '10 at 13:33
  • and yes I don't want to read the entire file in memory. The involved trace logs are quite big – Adrian Zanescu Jan 08 '10 at 13:34
  • @AZ: It's pretty tricky, because you basically have to read a buffer and work backwards finding line breaks, taking all kinds of things into account. I suggest you try to learn from the existing code :) – Jon Skeet Jan 08 '10 at 14:52