I´m trying to use the .net API to seek in a large data file. For some reason I am unable to make it work. Here is my code:
function check_logs{
$pos = 8192
$count = 1
$path = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Log\ERRORLOG.2'
$br = 0
$reader = [System.IO.File]::OpenText($path)
$reader.DiscardBufferedData()
$reader.BaseStream.Seek(0, [System.IO.SeekOrigin]::Begin)
for(;;){
$line = $reader.ReadLine()
if($line -ne $null){$br = $br + [System.Text.Encoding]::UTF8.GetByteCount($line)}
if($line -eq $null -and $count -eq 0){break}
if($line -eq $null){$count = 0}
elseif($line.Contains(' Error:')){
Write-Host "$line $br"
}
}
}
If I use 0 as a parameter for the seek method it seeks from the beginning as expected but it also writes 0 out to the console before it writes the lines read. Example:
0
2011-08-31 09:26:36.31 Logon Error: 17187, Severity: 16, State: 1. 4101
2011-08-31 09:26:36.32 Logon Error: 17187, Severity: 16, State: 1. 4489
2011-08-31 09:26:38.25 Logon Error: 17187, Severity: 16, State: 1. 4929
2011-08-31 09:26:38.25 Logon Error: 17187, Severity: 16, State: 1. 5304
2011-08-31 09:26:43.75 Logon Error: 17187, Severity: 16, State: 1. 6120
If I try to seek using 4096 instead of 0 it only writes out:
4096
I would have thought it would write out the same lines as the first one did apart from the first two.
Can someone see the problem? I had another question that got me to this. For further background see this
EDIT: Still trying to figure this out. Does anyone know where else I could try to find information regarding this problem? Is it possible to send questions to the Microsoft scripting guy?
Best regards
Gísli