我想使用C sharp总结“GSA Search”的“经过时间”总计:
以下是我的日志文件:
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);
}
答案 0 :(得分: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);
输出ms: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);