C#:使用正则表达式记录解析日期/时间,当异常> 1秒时,我想将整行保存到新文件

问题描述 投票:0回答:2

我在C#中编写了一个程序,帮助我解析大型日志(大约2 GB或更多)。到目前为止,我已经只输出了它的时间:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace LogParser
{
class Program
{
    static void Main(string[] args)
    {
        int counter = 0;
        string line;
        System.IO.StreamReader file = new System.IO.StreamReader(@"C:\\Users\\Jan\\Desktop\\api_fiter_sql.log");
        Console.WriteLine("Profesionalno branje logov se začenja: ");
        System.Threading.Thread.Sleep(2500);
        while ((line = file.ReadLine()) != null)
        {
            counter++;
            var regex = new Regex(@"\d{2}\:\d{2}:\d{2}.\d{4}");
            foreach (Match m in regex.Matches(line))
            {
                DateTime dt;
                if (DateTime.TryParseExact(m.Value, "HH:mm:ss.ffff", null, DateTimeStyles.None, out dt))
                {
                    Console.WriteLine(dt.ToString("HH:mm:ss.ffff"));
                }
            }
        }
        Console.WriteLine("Branje logov je končano. Prebrali smo: " + counter + " vrstic");
        Console.ReadKey();
    }
}
}

现在,一行日志看起来像这样:

<SQL > <TID: 0000000449> <RPC ID: 0000000000> <Queue: Admin     > <Client-RPC: 390600   > <USER:                                              > <Overlay-Group: 0         > /* Mon Feb 26 2018 13:52:08.4510 */ OK

我想做的是;如果超时(两次之间超过一秒),我希望程序导出该数据(两行;例如10:10:10.0000 - > 10:10:15.0000)让我们说一个.csv或.txt(无所谓)。

我该怎么做呢?我考虑过使用if语句,但我不知道从哪里开始。

c# regex parsing logging
2个回答
0
投票

你可以使用在while循环之外定义的变量,并且在你输入当前line的那一刻结束时,前面的变量总是一个小的类对象,而line和dt已经被解析了一些。下面是一些伪代码

string previousLine;
DateTime previousDt;
var timeOutList = new List<Tuple<string, string>>();
while ((line = file.ReadLine()) != null) {
  //regex parsing, cast to DateTime

  if ((dt-previousDt).TotalSeconds > 5)
  {
     timeOutList.Add(new Tuple<string, string>(previousLine, line));
  }

  previousLine = line;
  previousDt = dt;
}

//do something with the timeOutList like saving it to a file

0
投票

我已经解决了!如果有人想学习,这就是代码现在的样子。一位朋友帮了我很大的忙。

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace LogParser
{
class Program
{
    static void Main(string[] args)
    {
        Console.OutputEncoding = System.Text.Encoding.UTF8;
        int counterLine = 0;
        int counterTimeout = 0;
        string line = String.Empty;
        string previousLine = String.Empty;
        DateTime previousDt = DateTime.MaxValue;
        Regex regex = new Regex(@"\d{2}:\d{2}:\d{2}\.\d{4}");

        try
        {
            System.IO.StreamReader file = new 
 System.IO.StreamReader(args[0]);
            Console.WriteLine("Profesionalno branje logov se začenja:\n");
            StreamWriter writer = new 
 StreamWriter("C:\\Users\\Jan\\Desktop\\log.txt", true);

            while ((line = file.ReadLine()) != null)
            {
                counterLine++;
                foreach (Match m in regex.Matches(line))
                {
                    DateTime dt = new DateTime();
                    if (DateTime.TryParseExact(m.Value, "HH:mm:ss.ffff", null, DateTimeStyles.None, out dt))
                    {
                        if ((dt - previousDt).TotalSeconds > 1)
                        {
                            counterTimeout++;
                            Console.WriteLine(previousLine);
                            Console.WriteLine(line);
                            writer.WriteLine(previousLine);
                            writer.WriteLine(line);
                        }
                        previousLine = line;
                        previousDt = dt;
                    }
                }
            }
            file.Close();
            writer.Close();
            Console.WriteLine("\nBranje logov je končano. Prebrali smo: {0} vrstic ter izpisali " +
                              "{1} vrstic, kjer je bil timeout v datoteko.", counterLine, counterTimeout);
        }
        catch (Exception e)
        {
            Console.OpenStandardError();
            Console.WriteLine(e.Message);
        }
        if (args.Length < 1)
        {
            Console.OpenStandardError();
            Console.WriteLine("Uporaba: {0} LOG_FILE", AppDomain.CurrentDomain.FriendlyName);
            Console.ReadKey();
            return;
        }
    }
}
}
© www.soinside.com 2019 - 2024. All rights reserved.