Read log file from last read position

六眼飞鱼酱① 提交于 2019-12-08 12:57:46

问题


This is my working code, I have a filewatcher monitoring my log file, when the file updates (it updates a lot) it reads the log and outputs the lines that comply with the regex to a textbox. The problem with it is that it reads the file from the beginning and re-prints the regexed lines again so I get repeated data in the textbox. I also don't know if I have it setup correctly to run the file read from a separate thread so my program don't "freeze" while reading large log files.

private void btnStart_Click(object sender, EventArgs e)
{
    if ((Properties.Settings.Default.setting_logfile != "") && (Properties.Settings.Default.setting_logfolder != ""))
    {
        if (btnStart.Text == "Start Parsing")
        {
            // change text on button and switch status image
            btnStart.Text = "Stop Parsing";
            pbStatus.Image = Properties.Resources.online;

            new Thread(() => Run()).Start();
        }
        else
        {
            btnStart.Text = "Start Parsing";
            pbStatus.Image = Properties.Resources.offline;
            fsWatcher.EnableRaisingEvents = false;
        }
    }
    else
    {
        tbOutput.Text = "Please select a log file to parse.";
    }
}

private void Run()
{
    try
    {
        fsWatcher.Changed += new FileSystemEventHandler(OnChanged);
        fsWatcher.EnableRaisingEvents = true;
    }
    catch (Exception ex)
    {
        MessageBox.Show("An error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

private void OnChanged(object source, FileSystemEventArgs e)
{
    string line;

    Regex pattern = new Regex(@"\[[\w :]+\]\s<SYSTEMWIDE_MESSAGE>:");

    Stream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    StreamReader streamReader = new StreamReader(stream);

    while ((line = streamReader.ReadLine()) != null)
    {
        Match match = pattern.Match(line);
        if (match.Success)
        {
            tbOutput.AppendText(line + "\r\n");
        }
    }
    streamReader.Close();
    stream.Close();
}

回答1:


You could remember the last position that you read at and start from there.

 Regex pattern = new Regex(@"\[[\w :]+\]\s<SYSTEMWIDE_MESSAGE>:");
 long lastPosition = 0;

 private void OnChanged(object source, FileSystemEventArgs e)
 {
   string line;


   Stream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.Read,     FileShare.ReadWrite);
   stream.Seek(lastPosition, SeekOrigin.Begin);
   StreamReader streamReader = new StreamReader(stream);

   while ((line = streamReader.ReadLine()) != null)
   {
      if (pattern.Match(line).Success)
      {
        tbOutput.AppendText(line + Environment.NewLine);
      }
   }
   lastPosition = stream.Position;  // may need to subtract 1!!!
   streamReader.Close();
   stream.Close();
}


来源:https://stackoverflow.com/questions/32707104/read-log-file-from-last-read-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!