[英]Advice on appropriate data structure
我有两条数据:
同一个日志条目可以在一台机器上出现多次,也可以在多台机器上出现。 例如:
机号 | 事件字符串 |
---|---|
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 来存储一些事件信息:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.