Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,

I have the following log file:

2018-03-20 16:28:09,333 INFO [1] Luhn_Check.ResultForm.btnexport_Click - guy1 : generated 50 transactions.
2018-03-20 16:29:09,333 INFO [1] Luhn_Check.ResultForm.btnexport_Click - guy2 : generated 50 transactions.
2018-03-20 16:30:09,333 INFO [1] Luhn_Check.ResultForm.btnexport_Click - guy3 : generated 50 transactions.
2018-03-20 16:31:09,333 INFO [1] Luhn_Check.ResultForm.btnexport_Click - guy1 : generated 50 transactions.
2018-03-20 16:31:10,333 INFO [1] Luhn_Check.ResultForm.btnexport_Click - guy1 : generated 50 transactions.



someone can help me to generate the statistics for example:

month guy transactions
March 1 150
March 2 50
March 3 50

What I have tried:

I search through google to find something but was not successfull.
Posted
Updated 28-Mar-18 12:01pm
Comments
Mehdi Gholam 23-Mar-18 11:19am    
Seems simple enough, where are you stuck?
Joe Doe234 29-Mar-18 1:45am    
i dont have an idea from where im going to begin and how im going to pull certain data from a txt file. im still a student and will appreciate if you help me thanks.
Richard MacCutchan 23-Mar-18 11:42am    
Plenty of C# samples around the internet: TextStream, String etc.

1 solution

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;

/// <summary>
/// Example of using the TextFileDataSet to read the contents of a log file
/// and aggregate some values.
/// You can get the TextFileDataSet at https://github.com/cvanbergen/TextFileDataSet 
/// and read more about it at https://www.codeproject.com/Articles/22400/Converting-text-files-CSV-to-DataTable
/// </summary>
class Program
{
    static void Main(string[] args)
    {
        // Create an instance of MyTextFileDataSet
        // You can get the TextFileDataSet at https://github.com/cvanbergen/TextFileDataSet            
        var MyTextFileDataSet = new TextFileDataSet.TextFileDataSet();

        // open the file, in this case I assume that the name is Logfile.txt and it is in the 
        // same folder as where this executes
        using (var fileStream = new FileStream("Logfile.txt", FileMode.Open, FileAccess.Read))
        {
            // specify the regular expression for validating and recognising columns
            // this expression is already tailored for your log file sample
            MyTextFileDataSet.ContentExpression = new Regex(@"^(?<date>[^ ]+).+INFO \[1\] Luhn_Check.ResultForm.btnexport_Click -(?<name>[^:]+): generated (?<transactions>[0-9]+) transactions.$", RegexOptions.Multiline);
            // fill the dataset
            MyTextFileDataSet.Fill(fileStream);
        }

        // Create a collection of LogItems that we can later on show
        var logItems = new List<LogItem>();

        // Get all the rows from the first table in the dataset
        var rows = MyTextFileDataSet.Tables[0].AsEnumerable();
        // Iterate over all the rows and create a LogItem entry or 
        // update the 'Transactions' amount
        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;
            }
        }

        // Now output your LogItems.
        // Hey, it is a collection, you can sort it any way you like 
        logItems
            .OrderBy(n => n.Date)
            .ThenBy(n => n.Name)
            .ToList()
            .ForEach(n =>
                Console.WriteLine($"{n.Date} {n.Name} {n.Transactions}"));

        // Just keep the console window open ;-)
        Console.Read();
    }

}

/// <summary>
/// A simple class for your LogItem
/// </summary>
class LogItem
{
    public DateTime Date { get; set; }
    public string Name { get; set; }
    public int Transactions { get; set; }
}
 
Share this answer
 
Comments
Joe Doe234 29-Mar-18 7:42am    
thanks i will :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900