读取文本文件中的行块
Read block of lines in text file
我正在编写一个程序来读取日志文件。每个条目都以时间戳开头,除非出现错误,在这种情况下,我会有多行错误消息并且没有时间戳。
文件如下所示:
20190207 14:23:10.123 info Read input
20190207 14:23:11.001 info connecting to database
20190207 14:23:17.101 error truncating the table customer. Error code XXXX
the file was blocked.
我想将每个条目及其时间戳、事件类型和消息存储在具有三列的 table 中,一列用于时间戳(日期时间),另一列用于事件 (info/warning/error),以及文本列(数据类型文本)。
如何遍历文件并读取所有条目,包括有时出现在多行中的错误消息?
使用 ReadLine 来读取每一行。
例如:
while((line = file.ReadLine()) != null)
{
// some code here
}
现在,对于每一行,您必须找到第一次出现的 space (" "),在该索引处拆分,如果它是有效日期(使用特定格式解析),则将其用作日志。
否则将行保留到临时列表,直到找到另一个日期
你可以做的是使用Regex
来尝试匹配日志的每一行。如果匹配,则创建条目,否则将该行附加到现有条目。我将提供代码来尝试解释方法...
//strings for simplification, in your code you should use DateTime and parse properly
public class Entry
{
public string Timestamp { get; set; }
public string Type { get; set; }
public string Description { get; set; }
}
然后你可以定义regular expression
来捕获日志行。我使用组来完成此操作,因此更容易从中提取数据。请注意,您应该添加您期望的所有类型,我只使用 info|error|warning
.
//you should define all types you expect in log, I just put info and error
string LogLineRegex = @"(?<date>\d{4}\d{2}\d{2} \d{2}:\d{2}:\d{2}.\d{3}) (?<type>info|error|warning) (?<text>.*)";
然后读取日志的每一行:
- 匹配 - 创建新条目
- 不匹配 - 将行附加到滚动条目,假设您有错误
日志解析示例
Entry rollingEntry = null;
foreach (var line in log)
{
var match = Regex.Match(line, LogLineRegex);
if (match.Success)
{
if (rollingEntry != null) { entries.Add(rollingEntry); }
rollingEntry = new Entry{
Timestamp = match.Groups["date"].ToString(),
Type = match.Groups["type"].ToString(),
Description = match.Groups["text"].ToString() };
}
else
{
if (rollingEntry != null) { rollingEntry.Description += $"{Environment.NewLine}{line}"; }
}
}
我正在编写一个程序来读取日志文件。每个条目都以时间戳开头,除非出现错误,在这种情况下,我会有多行错误消息并且没有时间戳。
文件如下所示:
20190207 14:23:10.123 info Read input
20190207 14:23:11.001 info connecting to database
20190207 14:23:17.101 error truncating the table customer. Error code XXXX
the file was blocked.
我想将每个条目及其时间戳、事件类型和消息存储在具有三列的 table 中,一列用于时间戳(日期时间),另一列用于事件 (info/warning/error),以及文本列(数据类型文本)。
如何遍历文件并读取所有条目,包括有时出现在多行中的错误消息?
使用 ReadLine 来读取每一行。 例如:
while((line = file.ReadLine()) != null)
{
// some code here
}
现在,对于每一行,您必须找到第一次出现的 space (" "),在该索引处拆分,如果它是有效日期(使用特定格式解析),则将其用作日志。 否则将行保留到临时列表,直到找到另一个日期
你可以做的是使用Regex
来尝试匹配日志的每一行。如果匹配,则创建条目,否则将该行附加到现有条目。我将提供代码来尝试解释方法...
//strings for simplification, in your code you should use DateTime and parse properly
public class Entry
{
public string Timestamp { get; set; }
public string Type { get; set; }
public string Description { get; set; }
}
然后你可以定义regular expression
来捕获日志行。我使用组来完成此操作,因此更容易从中提取数据。请注意,您应该添加您期望的所有类型,我只使用 info|error|warning
.
//you should define all types you expect in log, I just put info and error
string LogLineRegex = @"(?<date>\d{4}\d{2}\d{2} \d{2}:\d{2}:\d{2}.\d{3}) (?<type>info|error|warning) (?<text>.*)";
然后读取日志的每一行:
- 匹配 - 创建新条目
- 不匹配 - 将行附加到滚动条目,假设您有错误
日志解析示例
Entry rollingEntry = null;
foreach (var line in log)
{
var match = Regex.Match(line, LogLineRegex);
if (match.Success)
{
if (rollingEntry != null) { entries.Add(rollingEntry); }
rollingEntry = new Entry{
Timestamp = match.Groups["date"].ToString(),
Type = match.Groups["type"].ToString(),
Description = match.Groups["text"].ToString() };
}
else
{
if (rollingEntry != null) { rollingEntry.Description += $"{Environment.NewLine}{line}"; }
}
}