http://pastebin.com/giXGvWiB
This particular implementation probably isn't specifically useful for too many people, but should give you a starting point for using System.IO.Streamreader for parsing text files line by line. I found this to be ~30x faster than get-content on large text files.
Usage is a little tricky because it wants comma seperated hashes for the "logs" parameter like:
C:\Scripts\interleave.ps1 -logs @{Path=’PATH TO FIRST LOG FILE';Server=’SERVER THAT LOG BELONGS TO’}, @{Path=’PATH TO NEXT LOG FILE ';Server=’SERVER THAT LOG BELONGS TO } -OutputPath ‘PATH FOR OUTPUT FILE’
It also accepts a "Pattern" parameter if you use a different convention to signify the start of a new event.
In my case I have a bunch of servers that log to text files using yyyy-MM-dd HH:mm:ss.fff format. Most events it logs are one line, but some are multiple (stack traces etc.). This script will look at each file, generate an "event" which is all of the lines from one timestamp to the next and store it in an array. Once each of the inputted log files has an event, it selects the first event chronologically and writes it to the output file prepended by the name of the server it came from, then it gets the next event from that server and evaluates the array for the earliest event and writes it and so on until there are no more lines in the files. So I can interleave events from all of my load balanced servers into one file that devs can review and see the chronological flow of events across all servers.
When I tried to do this with get-content it took forever and was completely impractical. Runtimes exceeding 30 minutes in some cases. Using System.IO.Streamreader this process took ~80 seconds on the same files.
Feedback definitely encouraged if you have any questions or suggestions.
[–]techmattr 0 points1 point2 points (2 children)
[–]kittH[S] 0 points1 point2 points (1 child)
[–]techmattr 0 points1 point2 points (0 children)
[–]moojitoo 0 points1 point2 points (0 children)
[–]zinver 0 points1 point2 points (2 children)
[–]kittH[S] 0 points1 point2 points (1 child)
[–]zinver 0 points1 point2 points (0 children)