From 88aee14975985d17660e6c5070af25e48fbc95d8 Mon Sep 17 00:00:00 2001 From: dcx25 <17374954+dcx25@user.noreply.gitee.com> Date: Sun, 2 Aug 2026 11:14:33 +0800 Subject: [PATCH] feat: complete basic functions --- docs/01-basic/report.md | 45 +++++++++++++++++++++++ src/LogParser/Models/LogEntries.cs | 4 +- src/LogParser/Parser/LineParser.cs | 43 ++++++++++++++++++---- src/LogParser/Visitors/KeyValueVisitor.cs | 23 +++++++++++- 4 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 docs/01-basic/report.md diff --git a/docs/01-basic/report.md b/docs/01-basic/report.md new file mode 100644 index 0000000..21b9dfe --- /dev/null +++ b/docs/01-basic/report.md @@ -0,0 +1,45 @@ +# 问答题报告 + +### (Q1.1) + +在给出的代码框架 `Parser` 中: + +* 哪条语句或哪几条语句将日志按逗号进行分割?代码中,我们是如何指定每一行的第几个字段代表何种意义的? +> using var csv = new CsvReader(logFile, config);配合循环使用的csv.GetRecords(); +>在LogRecordMap中一一指定对应 + +* 在对日志中 JSON 格式的 `message` 字段进行读取时,我们是在哪个方法内用哪几条语句判断这一行日志的种类(Call / Request / Internal)的? +> LineParser 类的 ParseLine 方法,if(root.TryGetProperty("event", out var eventElement))语句,再用swith判断 + +* 在确定了日志种类后,我们是调用了哪个库方法对 JSON 进行解析的? +> System.Text.Json 库中的 JsonSerializer.Deserialize(...) 方法 + +* 进一步,我们的框架代码是如何防止日志中有字段缺失的?(例如所给的 Call 日志的 `message` 中缺失 `request_id` 字段) +> 在每个属性前面都强制加上了 [property: JsonRequired] 特性标签 + +* 更进一步,日志中的 JSON 的键是 `abc-def` 命名法(称为烤串命名法),而我们的解析结果却是放在 `AbcDef` 命名法(称为大驼峰命名法)的属性里,我们的框架代码中是如何告诉 JSON 解析器完成这一命名法转换的? +> 框架代码事先创建了一个名为 options 的配置变量,并在其中设置了 JsonNamingPolicy.KebabCaseLower 这一规则。在调用 JsonSerializer.Deserialize 提取数据时,代码将这个 options 作为参数交给了解析工具。 + +--- + +### (Q1.2) + +以一个 Call 事件的解析结果为例,当调用 `KeyValueVisitor` 的 `Dump` 方法后,都有哪些方法被调用?请补充完整如下的方法调用链(.NET 内置库无需写出): + ++ `Dictionary KeyValueVisitor.Dump(LogEntry entry)` ++ > TResult CallLogEntry.Accept(ILogEntryVisitor visitor) ++ > Dictionary KeyValueVisitor.Visit(CallLogEntry entry) + + +--- + +### (Q1.3) + +本次作业中,你是否使用了 AI?根据你的使用情况,在以下 (Q1.3.a) (Q1.3.b) 两个问题中选择一题作答: + + +#### (Q1.3.b) +如果使用了 AI,你给予 AI 的提示词是什么?你认为 AI 给出的解答、你完全凭借传统搜索引擎以及自己的能力能够写出的解答之间,AI 的解答比你好在哪?AI 又有哪些解答是存在问题的,或者至少是不如你自己的解答的?给出你的理由。 ++ > 提示词上,在完成代码部分,我只是把ai当做搜索引擎使用,去解释一些我看不懂的C#内置函数,然后再用自己能力补充代码,不过在q1.1中第1,4,5个问题,我确实完全不了解c#内置的库是什么,让ai先给出了解答再自己借助ai去了解; ++ > 代码中,ai的解答和我几乎相同;在问答题中,ai的解答比我更具有专业性,对整个项目的把握比我更透彻 ++ > 我让只让ai给出了q1.1中1,4,5题超出我能力范围之外的解答,这些解答核实没发现什么大问题,其他的ai解答和我大致相同,我暂时未发现ai明显不如自己解答的情况。 \ No newline at end of file diff --git a/src/LogParser/Models/LogEntries.cs b/src/LogParser/Models/LogEntries.cs index 69edbc0..e4e9bbc 100644 --- a/src/LogParser/Models/LogEntries.cs +++ b/src/LogParser/Models/LogEntries.cs @@ -54,7 +54,7 @@ public sealed record RequestLogEntry( { public override TResult Accept(ILogEntryVisitor visitor) { - throw new NotImplementedException("TODO: T1.2"); + return visitor.Visit(this); } } @@ -69,7 +69,7 @@ public sealed record InternalLogEntry( { public override TResult Accept(ILogEntryVisitor visitor) { - throw new NotImplementedException("TODO: T1.2"); + return visitor.Visit(this); } } diff --git a/src/LogParser/Parser/LineParser.cs b/src/LogParser/Parser/LineParser.cs index 0475f6b..c20338e 100644 --- a/src/LogParser/Parser/LineParser.cs +++ b/src/LogParser/Parser/LineParser.cs @@ -1,4 +1,5 @@ -using LogParser.Models; +using CsvHelper; +using LogParser.Models; using System.Text.Json; using System.Text.Json.Serialization; @@ -16,8 +17,8 @@ public static LogEntry ParseLine(LogRecord logRecord) return eventElement.GetString() switch { "call" => LineParser.CreateCall(logRecord), - "request" => throw new NotImplementedException("TODO: T1.2"), - "internal" => throw new NotImplementedException("TODO: T1.2"), + "request" => LineParser.CreateRequest(logRecord), + "internal" => LineParser.CreateInternal(logRecord), _ => throw new FormatException($"Unknown event type: {eventElement.GetString()} in log message: {logRecord.Message}") }; } @@ -50,12 +51,35 @@ private static LogEntry CreateCall(LogRecord logRecord) private static LogEntry CreateRequest(LogRecord logRecord) { - throw new NotImplementedException("TODO: T1.2"); + var requestMessage = JsonSerializer.Deserialize(logRecord.Message, options) + ?? throw new FormatException($"Failed to deserialize request message: {logRecord.Message}"); + return new RequestLogEntry( + LineNo: logRecord.LineNo, + Timestamp: DateTimeOffset.Parse(logRecord.Timestamp), + PodName: logRecord.PodName, + Severity: ParseSeverity(requestMessage.Severity), + RequestId: requestMessage.RequestId, + Method: requestMessage.Method, + Path:requestMessage.Path, + StatusCode:requestMessage.StatusCode + ); } private static LogEntry CreateInternal(LogRecord logRecord) { - throw new NotImplementedException("TODO: T1.2"); + var internalMessage = JsonSerializer.Deserialize(logRecord.Message, options) + ?? throw new FormatException($"Failed to deserialize internal message: {logRecord.Message}"); + var index=internalMessage.Exception.IndexOf(": "); + var exceptionMessage=internalMessage.Exception.Substring(index+2); + var exceptionName=internalMessage.Exception.Substring(0,index); + return new InternalLogEntry( + LineNo: logRecord.LineNo, + Timestamp: DateTimeOffset.Parse(logRecord.Timestamp), + PodName: logRecord.PodName, + Severity: ParseSeverity(internalMessage.Severity), + ExceptionMessage:exceptionMessage, + ExceptionName:exceptionName + ); } private static LogSeverity ParseSeverity(string severity) @@ -77,11 +101,16 @@ private record CallMessage( ); private record RequestMessage( - // TODO: T1.2 + [property: JsonRequired] string Severity, + [property: JsonRequired] string RequestId, + [property: JsonRequired] string Method, + [property: JsonRequired] string Path, + [property: JsonRequired] int StatusCode ); private record InternalMessage( - // TODO: T1.2 + [property: JsonRequired] string Severity, + [property: JsonRequired] string Exception ); } } diff --git a/src/LogParser/Visitors/KeyValueVisitor.cs b/src/LogParser/Visitors/KeyValueVisitor.cs index e5ceba2..c0ae12e 100644 --- a/src/LogParser/Visitors/KeyValueVisitor.cs +++ b/src/LogParser/Visitors/KeyValueVisitor.cs @@ -26,12 +26,31 @@ public Dictionary Visit(CallLogEntry entry) public Dictionary Visit(RequestLogEntry entry) { - throw new NotImplementedException("TODO: T1.3"); + return new Dictionary + { + ["LineNo"] = entry.LineNo.ToString(), + ["Timestamp"] = entry.Timestamp.ToString("O"), + ["PodName"] = entry.PodName, + ["Severity"] = entry.Severity.ToString(), + ["EventType"] = entry.EventType.ToString(), + ["RequestId"] = entry.RequestId, + ["Method"] = entry.Method, + ["Path"] = entry.Path, + ["StatusCode"] = entry.StatusCode.ToString(), + }; } public Dictionary Visit(InternalLogEntry entry) { - throw new NotImplementedException("TODO: T1.3"); + return new Dictionary{ + ["LineNo"] = entry.LineNo.ToString(), + ["Timestamp"] = entry.Timestamp.ToString("O"), + ["PodName"] = entry.PodName, + ["Severity"] = entry.Severity.ToString(), + ["EventType"] = entry.EventType.ToString(), + ["ExceptionName"] = entry.ExceptionName, + ["ExceptionMessage"] = entry.ExceptionMessage, + }; } } }