如何在解析日志文件时获取设置值 [英] How to get set value whilst parsing a log file
问题描述
您好我正在尝试通过我的应用程序中的RoboCopy方法从日志文件中获取复制的文件数量值。日志文件始终采用以下格式:
-------------------------- -------------------------------------------------- ---
ROBOCOPY ::适用于Windows的强大文件复制::版本XP010
------------------- -------------------------------------------------- ----------
开始于:10月10日星期四10:08:51
来源:\\\\
as\Dev_Code\ITA\Stats\11.6.4.15\CFI \Build \
Dest:C:\inetpub \ CFI \
文件:*。*
选项:*。* / FFT / NFL / TEE / S / E / COPY:DAT / Z / IS / R:5 / W:5
----------- -------------------------------------------------- -----------------
1 \\\\\\\Dev_Code\\\统计数据:\ 11.6.4.15 \ CFI \Build \
-------------------- ------- -------------------------------------------------- -
总复制跳过不匹配失败额外
目录:1 0 1 0 0 0
文件:1 1 0 0 0 0
字节:1.62 m 1.62 m 0 0 0 0
时间:0:00:03 0:00:02 0:00:00 0:00:00
速度:607364字节/秒。
速度:34.753兆字节/分钟。 />
结束时间:10月10日星期四10:08:59
所以我能够将文件读入流读取器,但有什么我可以做的,以确保我总是选择文件的最后一个实例:然后在Total列下的值即1.下面我试图拆分基于字符串它是制表符分隔但这是返回系统.String []
尝试
{
// 打开文件进行阅读。
使用(StreamReader r = new StreamReader( @ C:\ LogFile.log))
{
// 2。
// 读取每行直至EOF。
string 行;
while ((line = r.ReadLine())!= null )
{
// 3.
// 用行做事。
if (line.Contains ( Files))
{
String content = line.ToString();
char sep = ' \ t' 跨度>;
string [] splitContent = content.Split(sep);
Console.WriteLine(splitContent);
}
}
}
}
catch (例外)
{
throw ;
}
输出你的字符串数组:
< pre lang =c#> foreach ( string s in splitContent)
Console.WriteLine(s)
如果\t不起作用,是否有空格?试试这个:
splitcontent = System.Text.RegularExpressions.Regex.Replace(content, @ \s +, )。拆分(' < span class =code-string>');
用一个空格替换所有空格然后拆分字符串
这是我的完整工作示例:
静态 void Main()
{
try
{
// 打开文件进行阅读。
使用(StreamReader r = new StreamReade r( @ C:\ LogFile.log))
{
// 2.
// 读取每行直到EOF。
string 行;
while ((line = r.ReadLine())!= null )
{
// 3.
// 用行做事。
if (line.Contains ( Files))
{
if (!line.Contains( Files:*。*) )
{
String content = line.ToString();
string [] splitContent = System.Text.RegularExpressions.Regex.Replace(content, @ \s +, )。分割(' ');
// foreach(splitContent中的字符串s)
< span class =code-comment> // Console.WriteLine(s);
Console.WriteLine(splitContent [ 3 跨度>]);
}
}
}
}
}
catch (例外情况)
{
Console.WriteLine(ex.Message);
}
}
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:
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows :: Version XP010
-------------------------------------------------------------------------------
Started : Thu Oct 10 10:08:51 2013
Source : \\ad\nas\Dev_Code\ITA\Stats\11.6.4.15\CFI\Build\
Dest : C:\inetpub\CFI\
Files : *.*
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
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. Below I am trying to split the string based on it being tab delimited but this is returning System.String[]
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;
}
Output your array of strings like this:
foreach (string s in splitContent) Console.WriteLine(s)
If \t doesn't work, could it be there are spaces? Try this:
splitcontent = System.Text.RegularExpressions.Regex.Replace(content, @"\s+"," ").Split(' ');
It replaces all spaces with a single one and then splits the string
here's my full working example:
static void Main() { 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")) { if (!line.Contains("Files : *.*")) { String content = line.ToString(); string[] splitContent = System.Text.RegularExpressions.Regex.Replace(content, @"\s+", " ").Split(' '); //foreach (string s in splitContent) //Console.WriteLine(s); Console.WriteLine(splitContent[3]); } } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
这篇关于如何在解析日志文件时获取设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!