Darren
Darren

Reputation: 1

Advice on appropriate data structure

Background

I have two pieces of data:

The same log entry can occur multiple times on one machine and can occur on multiple machines. For example:

machineNumber eventString
1 LogExample1
2 LogExample1
1 LogExample1
4 LogExample3
3 LogExample2

What I want to do is store this data temporarily in some sort of data structure so I can format it into the follow eventString, NumberOfMachinesEffected, TotalNumberOfInstances before storing it as a CSV file.

With the above example it would be formatted like LogExample1, 2, 3.

Problem

I'm wondering if someone can recommend an efficient method to store the data before formatting it. I need to be able to iterate over it to be able to count total number off occurrences, total number of machines effected, for each eventString.

Requested Code

I was asked to include the code. I don't think it pertains to the problem as it is purely a design question.

namespace ConfigLogAnalyser
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public String fileName;
    public MainWindow()
    {
        InitializeComponent();
    }

    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
        openFileDialog.Filter = "Text files(*.txt) | *.txt";
        openFileDialog.InitialDirectory = "D:\\LogFiles"; //Testing only. Remove
        if (openFileDialog.ShowDialog() == true)
        {
            //ensure it is a text file
            fileName = openFileDialog.FileName;
            if(!ProcessLogFile(fileName))
            {
                MessageBox.Show("Issue reading file: " + fileName);
            }

        }
    }
    //to be removed
    private bool ProcessLogFile(string fileName)
    {
        if (!ReadLogFile(fileName))
        {
            return false;
        }
       
        return true;
    }
    //Why does this need to be a bool
    private bool ReadLogFile(string fileName)
    {
        const Int32 BufferSize = 1024; //Changing buffersize will effect performance.
        using (var fileStream = File.OpenRead(fileName))
        using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize))
        {
            String line;
            while ((line = streamReader.ReadLine()) != null)
            {
                ProcessLine(line);
            }
        }
        return true;
    }

    private void ProcessLine(string line)
    {
        /*Process Line -
        *
        * Possibly use a multimap to store each logEntry of interest and a pair <machineId, NoOfOccurences>
        * Problem. If an occurence happens twice by the same machine how do I make sure two isn't added to number of machines.
        *
        */
        throw new NotImplementedException();
    }
}

}

Upvotes: 0

Views: 57

Answers (1)

Vladislav Horbachov
Vladislav Horbachov

Reputation: 334

I recommend you to create your own class to store some event information:

class EventInfo
{
    public int MachineID { get; set; }

    public string LogMessage { get; set; }

    public DateTime EventTime { get; set; }
}

And then just create a list of EventInfo:

List<EventInfo> events = new List<EventInfo>();

C# List has quite good performance, and, in addition, using LINQ you can easily manipulate a data.

For example:

events.Where(item => item.MachineID == 1).Select(item => item.LogMessage);

This code is selecting all the events messages, related to the machine, with ID = 1

Upvotes: 1

Related Questions