Need to compare a time value RRS feed

  • Question

  • User514976284 posted


    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.


    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.

    Thursday, January 4, 2018 4:06 AM

All replies

  • User-1838255255 posted

    Hi SasikalaSankaran,

    According to your description, i think you could read the log from bottom to top, until read the text, that means if you read this line is null when date is 2018 08:30:23, you could try this logical code:

    string linedata= line date when date= 2018 08:30:23
    for(int index=linedata's index;index--;log.lines)
    start your batch method

    Also you say you log very one minute, so you could add one minute to the starttime, that will get the data. 

    Best Regards,

    Eric Du 

    Friday, January 5, 2018 9:00 AM