【问题标题】:How to get or set value while parsing a log file如何在解析日志文件时获取或设置值
【发布时间】:2013-10-17 09:41:55
【问题描述】:

您好,我正在尝试从我的应用程序中的 RoboCopy 方法生成的日志文件中获取文件复制值的数量。日志文件始终采用以下格式:


ROBOCOPY :: 强大的 Windows 文件复制 :: 版本 XP010

开始时间:2013 年 10 月 10 日星期四 10:08:51

来源:\ad\nas\Dev_Code\ITA\Stats\11.6.4.15\CFI\Build\ 目标:C:\inetpub\CFI\

Files : *.*

选项:. /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

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;
}

输出 min = System.String[]

【问题讨论】:

    标签: c# parsing logging


    【解决方案1】:

    您只需要选择数组中的 second 项,即

    Console.WriteLine(splitContent[1]);
    

    想想看,你的线数据是这样的

    Files :\t1\t1\t0\t0\t0\t0
    

    当您调用 content.Split(sep) 时,您的数组将如下所示

    0: "Files :"
    1: "1"
    2: "1"
    3: "0"
    4: "0"
    5: "0"
    6: "0"
    

    因此,如果您根据 进行映射,您总是希望提取位于索引 1 处的 Total 列(0 是标签列)。


    您甚至可以在此处使用 emum 以提高可读性,例如

    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]);
    

    【讨论】:

    • +1 建议使用有意义的enumconst int
    • 感谢您抽出宝贵时间帮助我 - 谢谢
    【解决方案2】:

    试试这个:

    foreach(var section in splitContent)
    {
        Console.Write("{0}\t", section);
    }
    Console.WriteLine();
    

    【讨论】:

    • 我试过添加这个,现在有 2 行输出: 文件:. 文件:1 1 0 0 0 0 我能做些什么来忽略第一行输出行并在第 2 行中提取第一个值,因为 /t 上的拆分似乎没有更正我收到此错误:索引超出了数组的范围。
    • @James 给出了更好的答案,为您的问题提供了解决方案
    • 字符串被空格分割,没有制表符分隔,谢谢您的建议!
    【解决方案3】:

    这是我的完整工作示例:

    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);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 2012-08-08
      相关资源
      最近更新 更多