User514976284 posted
Hi,
I have a log file having log details with date and time with 12 hour format. Logs will be captured for every minute daily. I have scheduled a batch to run every 10 minutes once and my program has to read 10 minutes prior content from the log file from the
batch executing time. I facing the challenge that if I subtract 10 minutes from the batch running time, the time value I am getting may vary in seconds that there may be a chance that there will not be any log data for that seconds.
Example:
My batch runs at Jan 4, 2018 08:40:23 AM, I need to check logs from Jan 4, 2018 08:30:23 AM, but if I don't have logs at 23 seconds I couldn't capture that line to start reading. So I want suggestion how to fetch + or - second value to find the line.
I have written like this to fetch the starting line from where I should start reading the file,
string lastLine = File.ReadLines(path).Last();
string startValue = lastLine.Substring(1, 25);
string timeValue = string.Format("{0:MMM dd, yyyy hh:mm:ss tt}", Convert.ToDateTime(startValue).AddMinutes(10));
Inside loop I have given like this,
if (index <= 0)
index = line.IndexOf(timeValue);
if (index > 0)
{ ...... }
Note: In Log it is not possible to change the Time format it has to be 12 hour format.
Thanks, Sasikala.