使用空格解析日志文件

时间:2014-02-05 15:44:06

标签: vb.net parsing datagridview logfile logfile-analysis

我正在尝试将logfile(保存为*.log文件的文本文件)解析为Visual Basic 2013 Express“DataGridView。日志文件使用空格作为分隔符,但日志的消息部分中包含空格。这是日志文件的布局:

  

日期时间ID连接Type_of_message(命令/响应/状态/等)   消息(可能包含许多空格(不是相同数量的空格)   每条消息))

每个日志条目都是文件中的一个单独行。

编辑:以下是an example of the log file

2014-02-03 15:35:29 9900 3 Status: Listing directory [[server folder]]
2014-02-03 15:35:46 9900 3 Status: Invalid character sequence received, disabling UTF-8. Select UTF-8 option in site manager to force UTF-8.
2014-02-03 15:35:46 9900 3 Status: Calculating timezone offset of server...
2014-02-03 15:35:46 9900 3 Command: mtime "[[file name]]"
2014-02-03 15:35:46 9900 3 Response: 1382557913
2014-02-03 15:35:46 9900 3 Status: Timezone offsets: Server: -14400 seconds. Local: -18000 seconds. Difference: -3600 seconds.
2014-02-03 15:36:18 9900 2 Status: Connected to [[server name]]
2014-02-03 15:36:18 9900 1 Status: Starting upload of [[local folder name/file name (includes 3 spaces)]]
2014-02-03 15:36:18 9900 1 Command: cd "[[server folder]]"
2014-02-03 15:36:18 9900 2 Status: Starting upload of [[local folder name/file name (includes 3 spaces)]]
2014-02-03 15:36:18 9900 1 Response: New directory is: "[[server folder]]"
2014-02-03 15:36:18 9900 1 Command: put "[[local folder name/file name (includes 2 spaces)]]"
2014-02-03 15:36:18 9900 2 Command: cd "[[server folder]]"
2014-02-03 15:36:18 9900 1 Status: local: [[local folder name/file name (includes 3 spaces)]]=> remote:[[server folder]]

1 个答案:

答案 0 :(得分:2)

它可能不漂亮,但它有效:

Dim line As String = "2014-02-03 15:35:29 9900 3 Status: Listing directory [[server folder]]"
Dim split As String() = line.Split(" "c)
Dim message As String = String.Join(" "c, split.Skip(5).ToArray)

假设您总共有6列,最后一列是您的消息。

编辑:正如@Andrew Morton所建议的那样,拆分可以重写(没有LINQ):

Dim split As String() = line.Split({" "c}, 6, StringSplitOptions.None)