0

我想仅使用 C sharp 对“GSA 搜索”的总“经过时间”求和:

以下是我的日志文件:

WX Search = Server:testserver User:vibsharm appGUID: wx Elapsed Time:975ms SaveSearchID:361
WX Search = Server:testserver User:vibsharm appGUID: wx Elapsed Time:875ms SaveSearchID:361
GSA Search = Server:testserver User:gulanand appGUID: wx Elapsed Time:890ms SaveSearchID:361
GSA Search = Server:testserver User:vibsharm appGUID: wx Elapsed Time:887ms SaveSearchID:361
GSA Search = Server:testserver User: gulanand appGUID: wx Elapsed Time:875.5ms SaveSearchID:361
GSA Search = Server:testserver User:vibsharm appGUID: wx Elapsed Time:877.6ms SaveSearchID:361

我试过的代码是:

string searchKeyword = "WX GSA Search";
            string fileName = @"C:\Users\karan\Desktop\Sample log file.txt";
            string[] textLines = File.ReadAllLines(fileName);

            List<string> results = new List<string>();

            foreach (string line in textLines)
            {
                if (line.Contains(searchKeyword))
                {
                    results.Add(line);
                }
            }

            string x = string.Join(",", results);
            List<string> time = new List<string>();
            Regex regex = new Regex(@"Elapsed Time:(?<timevalue>\d+(?:\.\d)?)ms");
            MatchCollection matches = regex.Matches(x);
            foreach (Match match in matches)
            {
                var value = match.Groups["timevalue"].Value;
                if (!time.Contains(value)) time.Add(value);
            }
4

1 回答 1

1

这里有一些东西可以让你开始

string text = @"WX Search = Server:testserver User:vibsharm appGUID: wx Elapsed Time:975ms SaveSearchID:361 WX Search = Server:testserver User:vibsharm appGUID: wx Elapsed Time:875ms SaveSearchID:361 GSA Search = Server:testserver User:gulanand appGUID: wx Elapsed Time:890ms SaveSearchID:361 GSA Search = Server:testserver User:vibsharm appGUID: wx Elapsed Time:887ms SaveSearchID:361 GSA Search = Server:testserver User: gulanand appGUID: wx Elapsed Time:875.5ms SaveSearchID:361 GSA Search = Server:testserver User:vibsharm appGUID: wx Elapsed Time:877.6ms SaveSearchID:361";
var elapsedTime = text.ToLower().Split(' ').Where(line => line.StartsWith("time"))
                  .Select(timeLine => decimal.Parse(timeLine.Split(':')[1].Replace("ms",String.Empty)))
                  .Sum( time => time);

以毫秒为单位的输出:5380.1

啊,我刚刚注意到你只说“GSA 搜索”。在这种情况下,您首先需要过滤“GSA 搜索”,然后应用上述代码。在您的代码中,您已经这样做了

string fileName = @"C:\Users\karan\Desktop\Sample log file.txt";
            string[] textLines = File.ReadAllLines(fileName);

            List<string> results = new List<string>();

            foreach (string line in textLines)
            {
                if (line.Contains(searchKeyword))
                {
                    results.Add(line);
                }
            }



   var elapsedTime = results.SelectMany(line => line.ToLower().Split(' '))
                     .Where(line => line.StartsWith("time"))
                     .Select(timeLine => decimal.Parse(timeLine.Split(':')[1].Replace("ms",String.Empty)))
                     .Sum( time => time);
于 2013-03-03T15:00:16.620 回答