Capstone A: the report
The report of the capstone is one table: one row per level, the number of requests and the total time. The lesson writes the input, parses it with the shape of the previous lesson, groups it and prints it, and the output is the report the following lessons extend.
The entries
Each line carries a level and a duration in milliseconds at its end. The program reads the file line by line, takes the last token apart at = and keeps the two values it needs:
var path = Path.Combine(Path.GetTempPath(), "aredel-report.log");
var raw = new[]
{
"2024-03-10T09:30:00Z INFO request id=1 ms=12",
"2024-03-10T09:30:01Z WARN request id=2 ms=240",
"2024-03-10T09:30:02Z ERROR request id=3 ms=90",
"2024-03-10T09:30:03Z INFO request id=4 ms=30",
"2024-03-10T09:30:04Z INFO request id=5 ms=18",
};
File.WriteAllLines(path, raw);
var entries = new List<LogLine>();
foreach (var line in File.ReadLines(path))
{
var parts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
var ms = int.Parse(parts[^1].Split('=')[1]);
entries.Add(new LogLine(parts[1], ms));
}
Console.WriteLine($"{"level",-6}{"count",6}{"total",6}");
foreach (var group in entries.GroupBy(e => e.Level).OrderBy(g => g.Key))
{
Console.WriteLine($"{group.Key,-6}{group.Count(),6}{group.Sum(e => e.Ms),6}");
}
Console.WriteLine($"overall {entries.Count} {entries.Sum(e => e.Ms)}");
File.Delete(path);
record LogLine(string Level, int Ms);
dotnet run
level count total
ERROR 1 90
INFO 3 60
WARN 1 240
overall 5 390
Three INFO entries total 60 milliseconds, the one WARN is 240 and the one ERROR is 90; the overall line counts five entries and 390 milliseconds. The empty space in the output is the padding, not a mistake: the widths of the format string produce it.
Exercises
- Add an Average column with one decimal place.
- Join the parser of the previous lesson to this program and skip a bad line.
- Sort the groups by total time instead of by level and print the same table.
- Print the level with a width of ten and see how the header changes.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does GroupBy return?
Answer
One group per key, each with the elements that share it
Each group has a Key, a Count and the elements to sum.
-
Why is the grouping sorted by key before it is printed?
Answer
The order of a grouping is not promised, and the table must be stable
A report that changes order between runs is hard to compare.
-
What does this program print?
var values = new[] { 3, 1, 2 }; Console.WriteLine(string.Join(",", values.OrderBy(x => x)));Answer
1,2,3
OrderBy sorts ascending by the value.
-
What does the width in {group.Key,-6} do?
Answer
Pads the key to at least six characters on the right
The minus sign left-aligns, and the string is not truncated.
-
What does this program print?
Console.WriteLine(new[] { 1, 2, 3 }.Sum());Answer
6
The sum of one, two and three is six.
-
What does a positive width in {value,6} do for a number?
Answer
Right-aligns it in six characters
The count and total columns line up on the right.
-
What does the width 4 do in the format item {42,4}?
Answer
Pads the number to four characters on the left
A positive width right-aligns, which is why the number ends the column.
-
How many INFO entries did the report count?
Answer
3
The measured table row is INFO 3 60.
-
What does this program print?
Console.WriteLine(new[] { 1, 2, 3 }.Count(x => x > 1));Answer
2
The values above one are two and three.
-
What does the last line of the report contain?
Answer
The overall count and the overall total
The measured line is overall 5 390.
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