Hi there,
A long time ago I wrote
Converting-text-files-CSV-to-DataTable. Not many of us still make use of them anymore, but every now and again I find cases where it would still be applicable. You could use the code below as a console application and use it on your log files. The regular expression used is tailored to your sample and the ouput is as you probably need it.
HTH Christiaan
using System;
using System.Linq;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
var MyTextFileDataSet = new TextFileDataSet.TextFileDataSet();
using (var fileStream = new FileStream("Logfile.txt", FileMode.Open, FileAccess.Read))
{
MyTextFileDataSet.ContentExpression = new Regex(@"^(?<date>[^ ]+).+INFO \[1\] Luhn_Check.ResultForm.btnexport_Click -(?<name>[^:]+): generated (?<transactions>[0-9]+) transactions.$", RegexOptions.Multiline);
MyTextFileDataSet.Fill(fileStream);
}
var logItems = new List<LogItem>();
var rows = MyTextFileDataSet.Tables[0].AsEnumerable();
foreach (var row in rows)
{
var date = DateTime.Parse(row.Field<string>("date"));
var name = row.Field<string>("name").Trim();
var transactions = int.Parse(row.Field<string>("transactions"));
var logItem = logItems
.Where(n => n.Date == date)
.Where(n => n.Name == name)
.FirstOrDefault();
if (logItem == null)
{
logItems.Add(new LogItem
{
Date = date,
Name = name,
Transactions = transactions
});
}
else
{
logItem.Transactions += transactions;
}
}
logItems
.OrderBy(n => n.Date)
.ThenBy(n => n.Name)
.ToList()
.ForEach(n =>
Console.WriteLine($"{n.Date} {n.Name} {n.Transactions}"));
Console.Read();
}
}
class LogItem
{
public DateTime Date { get; set; }
public string Name { get; set; }
public int Transactions { get; set; }
}