Objective:
Get the last line or 2 of a log file that's anywhere between 1kb and 600kb on average.
The initial parse function:
Seperate functions that handle parsing a specific line:Code:public void ParseLogFile(string fullLogPath) { string[] logLines = File.ReadAllLines(fullLogPath, Encoding.UTF7); int lastLineIndex = logLines.GetUpperBound(0); if (logLines[lastLineIndex - 1].IndexOf("±â") > 0) ParseHeader(logLines[lastLineIndex - 1].Substring(logLines[lastLineIndex - 1].LastIndexOf(": ") + 2)); if (logLines[lastLineIndex].IndexOf("¹ø") > 0) ParseBody(logLines[lastLineIndex].Substring(logLines[lastLineIndex].LastIndexOf(": ") + 2)); if (logLines[lastLineIndex].IndexOf("ÅÏ :") > 0) ParseFooter(logLines[lastLineIndex].Substring(logLines[lastLineIndex].LastIndexOf(": ") + 2)); }
The others are similiar to the one posted above. Don't need to post it.Code:private void ParseHeader(string logLine) { string[] headerInfoArray; string startAtSection = string.Empty; string[] splitter = { " " }; startAtSection = logLine.Substring(logLine.LastIndexOf(":") + 1); headerInfoArray = startAtSection.Split(splitter, StringSplitOptions.None); Header.Title = headerInfoArray[0]; Header.Tags = headerInfoArray[1]; }
ParseLogFile() gets called everytime the filesystemwatcher event triggers, which is usually every 5-15 seconds.
Is there any way I could make this better?
Edit:
Might as well throw this question in here too rather than make a new post. At the moment in that filesystemwatcher event I am using Thread.Sleep(200) before I parse the file because without it I kept randomly getting a "file is in use by another process, you can't read this file" error.
Also without the sleep thrown in there the log file was actually getting corrupted occasionally. Once in a while it would skip a few lines that should have been logged.
Is there a more proper way of telling my program to wait until the file has been closed by the log owner's app before I read it?


Reply With Quote