WebVeta - Advanced, unified, consistent search for your website(s), from content of your website(s), blogs(s). First 50 customers, who sign-up prior to 15/05/2024 get unlimited access to existing features, newer features for at least 1 year. Sign up now! https://webveta.alightservices.com/
Categories
.Net C#

File tailer in C#

As mentioned in a previous post – Cancelling all activities at ALight Technology And Services Limited. I am not seriously working on anything within ALight Technology And Services Limited, but I am still .Net developer. I have some internal logging and monitoring implemented. I have used Cloudwatch Agent and Promtail for ingesting logs into AWS Cloudwatch and Grafana. Both are great platforms but I wanted to implement a similar tool in C#. Here is some sample code, explanation capable of doing something similar.

First store the name of file, Creation Date, Size, Last Modified Date. These can be accessed from FileInfo object.

var fi = new FileInfo("path");
fi.CreationTimeUtc	
fi.LastWriteTimeUtc	
fi.Length

Now, in a loop read the file, if new file i.e CreationTime is different read from beginning. If Length or LastWriteTime are different but same CreationTime, do a seek.

long seek = 0;

while(<some condition>)
{
   using(sr = new StreamReader(fi.OpenRead()){
      sr.BaseStream.Seek(seek, SeekOrigin.Begin);
      var data = await sr.ReadToEndAsync();
      seek += data.Length;
      // Do something with data
      // Store filename, seek in some persistent storage like a file
}

The above code block shows some sample code for using seek, we would store the variable seek along with FileCreation, Update, Length. Even if the application is restarted, the application would work properly.

I don’t have any fake aliases, nor any virtual aliases like some of the the psycho spy R&AW traitors of India. NOT associated – “ass”, eass, female “es”, “eka”, “ok”, “okay”, “is”, “erra”, yerra, karan, kamalakar, diwakar, kareem, karan, sowmya, zinnabathuni, bojja srinivas (was a friend and batchmate 1998 – 2002), mukesh golla (was a friend and classmate 1998 – 2002), thota veera, uttam’s, bandhavi’s, bhattaru’s, thota’s, bojja’s, bhattaru’s or Arumilli srinivas or Arumilli uttam (may be they are part of a different Arumilli family – not my family).

Mr. Kanti Kalyan Arumilli

Arumilli Kanti Kalyan, Founder & CEO
Arumilli Kanti Kalyan, Founder & CEO

B.Tech, M.B.A

Facebook

LinkedIn

Threads

Instagram

Youtube

Founder & CEO, Lead Full-Stack .Net developer

ALight Technology And Services Limited

ALight Technologies USA Inc

Youtube

Facebook

LinkedIn

Phone / SMS / WhatsApp on the following 3 numbers:

+91-789-362-6688, +1-480-347-6849, +44-07718-273-964

+44-33-3303-1284 (Preferred number if calling from U.K, No WhatsApp)

kantikalyan@gmail.com, kantikalyan@outlook.com, admin@alightservices.com, kantikalyan.arumilli@alightservices.com, KArumilli2020@student.hult.edu, KantiKArumilli@outlook.com and 3 more rarely used email addresses – hardly once or twice a year.