Capstone A: parsing the lines

A parser must expect a file that does not follow the format: a line cut in half, a level nobody defined, a time in another zone. The rule of this lesson is that a bad line is reported with its number and the program continues, because one broken line must not lose the other four.

A file with a bad line

The program writes its own input, so the third line is wrong on every machine: it is not a log entry at all. The other four follow the format of the previous lesson. The listing is the whole program, and the sections below describe its parts:

var path = Path.Combine(Path.GetTempPath(), "aredel-parse.log"); var raw = new[] { "2024-03-10T09:30:00Z INFO started", "2024-03-10T09:30:01Z WARN slow request ms=120", "not a log line", "2024-03-10T09:30:02Z ERROR failed id=7", "2024-03-10T09:30:03Z INFO done", }; File.WriteAllLines(path, raw); var good = 0; var bad = 0; var number = 0; foreach (var line in File.ReadLines(path)) { number++; if (TryParse(line, out var entry)) { good++; } else { bad++; Console.WriteLine($"bad line {number} [{line}]"); } } Console.WriteLine($"good {good} bad {bad}"); File.Delete(path); static bool TryParse(string line, out LogLine entry) { entry = default!; var parts = line.Split(' ', 3, StringSplitOptions.RemoveEmptyEntries); if (parts.Length < 3) { return false; } if (!DateTime.TryParseExact(parts[0], "yyyy-MM-ddTHH:mm:ssZ", null, System.Globalization.DateTimeStyles.AdjustToUniversal, out var at)) { return false; } if (parts[1] is not ("INFO" or "WARN" or "ERROR")) { return false; } entry = new LogLine(at, parts[1], parts[2]); return true; } record LogLine(DateTime At, string Level, string Message);

dotnet run

bad line 3 [not a log line]
good 4 bad 1

TryParse

The parser is a static method with an out parameter, so a bad line is a false and not an exception. Three checks decide whether the line is complete:

Counting the bad lines

The loop numbers the lines as it reads them and counts both outcomes. A line that fails is printed with its number, and the program keeps going. The third line is the one that failed, and the count confirms that the other four were parsed. A parser that reports good 4 bad 1 is a program a user can trust with a file that is not perfect.

The pattern of a level

parts[1] is not ("INFO" or "WARN" or "ERROR") is a pattern: the constant patterns are alternatives, and is not negates the whole. It compiles to a comparison chain and is the shortest way to state the three allowed values. An enum would be the stronger model, and the report lesson groups by the string the file already has, so the capstone keeps the string and validates it here.

The next lesson turns these entries into a report with a group per level and a total per group.

Exercises

  1. Add a line with a bad time and a line with a bad level; print both numbers.
  2. Make the parser accept a lowercase level and explain the change.
  3. Return the reason of a failure instead of a boolean and print it.
  4. Read a file with a trailing empty line and see whether the parser counts it.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Why does the parser return a bool instead of throwing?

    Answer

    A bad line is an expected case that the loop reports and skips

    One broken line must not lose the other four.

  2. What does Split(' ', 3, RemoveEmptyEntries) give for the WARN line?

    Answer

    Three parts: time, WARN and the message

    The count of three keeps the message whole, and the empty entries remove the double space.

  3. What does this program print? var parts = "a b c".Split(' ', StringSplitOptions.RemoveEmptyEntries); Console.WriteLine(parts.Length);

    Answer

    3

    The double space produces no empty entry, so a, b and c remain.

  4. Which line number did the bad line get?

    Answer

    3

    The measured output is bad line 3.

  5. What does DateTime.TryParseExact need besides the text?

    Answer

    The exact format the text must have

    The format is yyyy-MM-ddTHH:mm:ssZ for this file.

  6. What does this program print? Console.WriteLine("not a log line".Split(' ').Length);

    Answer

    4

    There are four words in the sentence.

  7. What does the pattern parts[1] is not ("INFO" or "WARN" or "ERROR") do?

    Answer

    Rejects a level that is not one of the three

    The alternatives are the allowed values and is not negates the match.

  8. What does this program print? Console.WriteLine(DateTime.TryParseExact("x", "yyyy", null, 0, out _));

    Answer

    False

    The text does not have the shape of the format.

  9. Why does the loop number the lines as it reads them?

    Answer

    To report where the bad line is in the file

    The bad line 3 tells the reader where to look.

  10. What did the program count?

    Answer

    Four good lines and one bad line

    The measured output is good 4 bad 1.

The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev