我想仅使用 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);
}