2

我想数数没有。出现 ProductServices 和 Add Product 的行数。

日志是:

INFO [ProductServices] 添加产品已由 gulanand 对 id 为 424 () 的产品执行 INFO [ProductServices] 添加产品已由 gulanand 对 id 为 424 () 的产品执行id 424 () INFO [ProductServices] Update Product has been executed by gulanand on product with id 424 () INFO [ProductServices] Update Product has been executed by gulanand on product with id 424 () INFO [ProductServices] Add Product has been executed by gulanand 在 id 为 424 () 的产品上添加产品 gulanand 对 id 为 424 () 的产品执行了添加产品 gulanand 在 id 为 424 () 的产品上执行

我试过的代码是:

IEnumerable<string> textLines =
    Directory.GetFiles(@"C:\Users\karansha\Desktop\Ashish Logs\", "*.*")
                        .Select(filePath => File.ReadAllLines(filePath))
                        .SelectMany(line => line);

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

Regex r = new Regex(@"*ProductServices\sAdd Product");
foreach (string Line in textLines)
{
    if (r.IsMatch(Line))
    {
        users.Add(Line);
    }
}
//string[] textLines1 = new List<string>(users).ToArray();
int countlines = users.Count();
Console.WriteLine("ProductsCreated=" + countlines);
4

2 回答 2

1

这是你想要的吗?

 int count = File.ReadAllLines().Count(x=>x.Contains("MONKEY"));

Linq是Luvly!

于 2013-04-05T08:53:23.313 回答
0

这个怎么样:(对于每个文件)

string[] lines = File.ReadAllLines(filePath)
int count = lines.Count(input => input.Contains("ProductServices") && 
                                 input.Contains("Add Product"));
于 2013-04-05T08:51:37.157 回答