Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to monitor a text file and parse new lines?

Tags:

c#

sql-server

I wondering if there is a way to monitor a .txt file without locking it and have the newly appended lines parsed in order to be sent to a database.

I know FileSystemWatcher will monitor the txt file without locking it and notify me if a change has occurred. The problem is that it does not tell the lines of data that has been appended. All of the examples I have looked at exclude the part of displaying newly appended lines or doing any processing of the new data. My program has the following structure:

static void Main(string[] args)
{
      
  //string line;

  //createWatcher (FileSystemWatcher)
  foreach (string line in getLines(@"C://Documents/log.txt"))
  {
    string teststring = line;         
    string[] parts = line.Split(' ', ',', '-', '>', '[', ']');
    StringBuilder builder = new StringBuilder();
            
    foreach (string h in parts)
    {
      builder.Append(h).Append(" ");
    }

    string result = builder.ToString();
    string cleanedString = System.Text.RegularExpressions.Regex.Replace(result, @"\s+", " ");
    string trimString = cleanedString.Trim();
    trimString = trimString.Remove(trimString.Length - 7);

    //Console.WriteLine(trimString);

    //Process new lines (parse/split)
    //Connect and send to database
  }
}

So my objective is to monitor a .txt file without locking it.

  1. Read this file for existing data, and wait for appended data.
  2. I then want to parse this data and send it to a database.

Does anyone know how to best approach this? or have a good tutorial?

like image 494
Rick Avatar asked Oct 29 '25 17:10

Rick


1 Answers

Your best bet is for the code fired off of the file system watcher to keep track of the Position in the stream that's reading the file. Here's some pseudocode:

if(no stored position)
  start at beginning of file
else
{
  streamOfFile.Seek(storedPosition)
  read until end of file
  store stream.Postition
}
like image 88
Servy Avatar answered Oct 31 '25 07:10

Servy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!