微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

关于适当数据结构的建议

如何解决关于适当数据结构的建议

背景

我有两条数据:

  • machineNumber,它只是一台机器的 ID。
  • eventString 是日志中的一个条目。

同一日志条目可以在一台机器上多次出现,也可以在多台机器上出现。例如:

ma​​chineNumber eventString
1 日志示例1
2 日志示例1
1 日志示例1
4 日志示例3
3 日志示例2

我想做的是将这些数据临时存储在某种数据结构中,以便在将其存储为 CSV 文件之前将其格式化为后续的 eventString、NumberOfMachinesEffected、TotalNumberOfInstances。

在上面的例子中,它会被格式化为 LogExample1,2,3。

问题

我想知道是否有人可以推荐一种在格式化之前存储数据的有效方法。我需要能够对其进行迭代,以便能够计算每个 eventString 的发生次数、受影响的机器总数。

请求的代码

我被要求包含代码。我认为这与问题无关,因为它纯粹是一个设计问题。

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();
    }
}

}

解决方法

我建议您创建自己的类来存储一些事件信息:

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

    public string LogMessage { get; set; }

    public DateTime EventTime { get; set; }
}

然后只需创建一个 EventInfo 列表:

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

C# List 具有相当不错的性能,此外,使用 LINQ 可以轻松操作数据。

例如:

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

此代码正在选择与机器相关的所有事件消息,ID = 1

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。