解析日志文件时如何获取或设置值 [英] How to get or set value while parsing a log file
问题描述
我正在尝试从我的应用程序中的RoboCopy方法产生的日志文件中获取文件复制数量的值.日志文件始终采用以下格式:
Hi I am trying to get the number of files copied value from a logfile produce by a RoboCopy method within my application. The logfile is always in this format:
开始时间:2013年10月10日星期四10:08:51
Started : Thu Oct 10 10:08:51 2013
源:\ ad \ nas \ Dev_Code \ ITA \ Stats \ 11.6.4.15 \ CFI \ Build \ 目的地:C:\ inetpub \ CFI \
Source : \ad\nas\Dev_Code\ITA\Stats\11.6.4.15\CFI\Build\ Dest : C:\inetpub\CFI\
Files : *.*
选项:./FFT/NFL/TEE/S/E/COPY:DAT/Z/IS/R:5/W:5
Options : . /FFT /NFL /TEE /S /E /COPY:DAT /Z /IS /R:5 /W:5
1 \\ad\nas\Dev_Code\ITA\Stats\11.6.4.15\CFI\Build\
Total Copied Skipped Mismatch FAILED Extras
Dirs : 1 0 1 0 0 0
**Files : 1** 1 0 0 0 0
Bytes : 1.62 m 1.62 m 0 0 0 0
Times : 0:00:03 0:00:02 0:00:00 0:00:00
Speed : 607364 Bytes/sec.
Speed : 34.753 MegaBytes/min.
Ended : Thu Oct 10 10:08:59 2013
到目前为止,我已经能够将文件读入流读取器中,但是有什么我可以做的以确保我总是选择Files的最后一个实例:然后选择Total列下的值,即1
So far I am able to read the file into a streamreader but is there anything I can do to ensure I am always selecting the last instance of Files : and then the value under the Total column i.e. 1
try
{
// Open file for reading.
using (StreamReader r = new StreamReader(@"C:\LogFile.log"))
{
// 2.
// Read each line until EOF.
string line;
while ((line = r.ReadLine()) != null)
{
// 3.
// Do stuff with line.
if (line.Contains("Files"))
{
String content = line.ToString();
char sep = '\t';
string[] splitContent = content.Split(sep);
Console.WriteLine(splitContent);
}
}
}
}
catch (Exception)
{
throw;
}
最小输出= System.String []
Output at the min = System.String[]
推荐答案
您只需要在数组中选择 second 项,即
You just need to select the second item in the array i.e.
Console.WriteLine(splitContent[1]);
考虑一下,您的行数据看起来像
Think about it, your line data looks like
Files :\t1\t1\t0\t0\t0\t0
调用content.Split(sep)
时,您的数组将如下所示:
When you call content.Split(sep)
your array will look like
0: "Files :"
1: "1"
2: "1"
3: "0"
4: "0"
5: "0"
6: "0"
因此,如果您根据列进行映射,则始终希望提取位于索引1
(0
为标签列)的Total
列.
So if you map this in terms of columns you always want to pull the Total
column which is at index 1
(0
being the label column).
您甚至可以在此处使用Emem来提高可读性,例如
You could even use an emum here for readability e.g.
enum Columns
{
Label,
Total,
Copied,
Skipped,
Mismatch,
Failed,
Extra
}
...
string[] rowData = content.Split('\t');
Console.WriteLine(rowData[(int)Columns.Total]);
或使用const
避免强制转换
const int TotalCol = 1;
...
string[] rowData = content.Split('\t');
Console.WriteLine(rowData[TotalCol]);
这篇关于解析日志文件时如何获取或设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!