Search code examples
c#regexparsinglogfile

How to extract only date and time from a large logfile?


I'd like to extract just date and time values out of a log file.

Here are a couple of lines from the log file:

2022-05-22 13:51:52,689 STATUS [Thread-5] hoststate.HighwayMonitorObserverImpl.localHighwayStateChanged - Highway State Changed [LOCAL APP_HW] to FAILURE.
2022-05-22 13:51:54,448 STATUS [HostStateManager[0]] hoststate.HostStateManager.a - [0] high way state changes : sys1-a - [OK, OK, OK, null]->[DELAY, OK, OK, null]
2022-05-22 13:51:54,450 STATUS [HostStateManager[0]] hoststate.HostStateManager.a - [0] update necessary

Btw I'm trying to parse all dates from the log files into another file and I'm stuck at this moment, thanks to everyone who could help me.


Solution

  • For 250kb size file, you can use the below code using Regex.

    using System.Text.RegularExpressions;
    
    // read file
    string logFile = File.ReadAllText("abc.txt");
    
    // Use regex
    Regex regex = new Regex(@"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}");
    MatchCollection matches = regex.Matches(logFile);
    
    // print all dates
    foreach (Match match in matches)
    {
        Console.WriteLine(match.Value);
    }