【问题标题】:How to keep track of line number in files when its being updated continuosuly如何在文件不断更新时跟踪文件中的行号
【发布时间】:2018-08-19 20:26:14
【问题描述】:

我们正在通过 API 将 IIS 日志文件推送到 DB 以通过 SPLUNK 进行监控,但我们正在发送重复数据,因为我们的 C# 作业每五分钟运行一次,它会发送所有行

        // Read the file and display it line by line.  
        System.IO.StreamReader file =
        new System.IO.StreamReader(filepath);
        while ((line = file.ReadLine()) != null)
        {
          System.Console.WriteLine(line);
        counter++;
        }

        file.Close();
        System.Console.WriteLine("There were {0} lines.", counter);
        // Suspend the screen.  
        System.Console.ReadLine();

【问题讨论】:

  • 你能解释一下你的问题吗?
  • 向数据添加 ID 字段。我建议以日志 XML 格式制作每个条目。 xml 格式不正确,这意味着根目录中有一个元素数组,但它可以轻松读取结果中的字段。在日志文件中使用 Xml 非常普遍,并且非常容易解析。请参阅以下代码项目:codeproject.com/Articles/28752/Use-XML-for-Log-Files

标签: c# filereader


【解决方案1】:

也许你可以试试这个:

  1. 在运行开始之前,将日志文件中的行数存储在一个文件(例如 file1)中。这是一次性步骤。
  2. 在代码中的 while 循环之前,从 file1 读取行数到一个变量中,比如 lastCount
  3. 将 System.Console.WriteLine(line) 行更改为以下内容:

    if (counter > lastCount)
    System.Console.WriteLine(line);

  4. while循环后,将计数器的值存入file1

【讨论】:

    猜你喜欢
    • 2015-10-15
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 2014-05-21
    • 1970-01-01
    • 2011-08-26
    • 2011-02-27
    相关资源
    最近更新 更多