public static string ReadLastLine(Stream stream, Encoding encoding)
{
var buf = new byte[1024 * 128];
var seekPos = Math.Min(buf.Length, stream.Length - stream.Position);
stream.Seek(-seekPos, SeekOrigin.End);
var lastBlockLength = stream.Read(buf, 0, buf.Length);
int i = lastBlockLength - 2;
if (i < 0)
return null;
for (; i > 0; i--)
{
if (buf[i] == '\n')
{
i++;
break;
}
}
var lastLine = encoding.GetString(buf, i, lastBlockLength - i);
return lastLine;
}