tags:

views:

73

answers:

1

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.

+2  A: 

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
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)
AZ
and yes I don't want to read the entire file in memory. The involved trace logs are quite big
AZ
@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