public DateTime GetStartTime(IninLog log) { //Get first 20 characters for //Start.*this.Name ie Start Path/To/Log/mylogfile.ininlog string logName = log.Name.Replace(".zip", ".ininlog"); DateTime results; string[] lines = File.ReadAllLines(this.FilePath); Regex rgx = new Regex("Start.*" + logName); try { string startLine = lines.First(x => rgx.Match(x).Success); results = Convert.ToDateTime(startLine.Substring(0, 20)); } //if we can't find the time from the Journal, use the min of the file create time and earliest time in journal catch (InvalidOperationException) { DateTime firstEntry = Convert.ToDateTime(lines[0].Substring(0, 20)); DateTime fileCreation = File.GetCreationTime(log.FilePath); results = new DateTime(Math.Min(firstEntry.Ticks, fileCreation.Ticks)); } return results; }
/// <summary> /// Gets the end time for a particular log file /// </summary> /// <param name="log">Log file to get end time for</param> /// <returns></returns> public DateTime GetEndTime(IninLog log) { //Similar to GetStartTime but for End string logName = log.Name.Replace(".zip", ".ininlog"); DateTime results; string[] lines = File.ReadAllLines(this.FilePath); Regex rgx = new Regex("End.*" + logName); try { string endLine = lines.First(x => rgx.Match(x).Success); results = Convert.ToDateTime(endLine.Substring(0, 20)); } //if we can't find the time from the Journal, use the max of the file last modified time and latest time in journal catch(InvalidOperationException) { DateTime lastEntry = Convert.ToDateTime(lines.Last().Substring(0, 20)); DateTime fileLastModified = File.GetLastWriteTime(log.FilePath); results = new DateTime(Math.Max(lastEntry.Ticks, fileLastModified.Ticks)); } return results; }
/// <summary> /// Snip log files based off of start and end time /// </summary> /// <param name="logs">Logs to process</param> /// <param name="start">Start time to snip from</param> /// <param name="end">End time to stop snipping</param> public static void SnipLogs(List<IninLog> logs, DateTime start, DateTime end) { OutputFilePaths.Clear(); List<IninLog> currentLogs = new List<IninLog>(); foreach(string type in IninLog.SelectedLogTypes) { List<IninLog> logsOfType = logs.FindAll(x => x.Type == type); //No logs for that selected type within the time range if (logsOfType.Count == 0) { continue; } foreach (IninLog log in logsOfType) { //If the log is zipped, we'll need to unzip it if (log.IsZipped) { Packer.UnZip(log.FilePath, OutputFolder + "\\temp\\"); //Remove the zip from currentLogs replace with unzipped log IninLog l = new IninLog(OutputFolder + "\\temp\\" + Path.GetFileNameWithoutExtension(log.FilePath) + ".ininlog"); currentLogs.Add(l); } else { currentLogs.Add(log); } } RunLogSnip(currentLogs, start, end); currentLogs.Clear(); } }