0

我正在尝试制作一个工具来获取格式化的文本文件,该文件存储我们的日志信息并仅返回其中的某些部分。例如,日志文件如下所示:

[TimeStamp] <Config>: Configuration note is shown here
[TimeStamp] <Info>: Information is written here   
[TimeStamp] <Info>: More Information
[TimeStamp] <Step>: A generated step is writing a message
[TimeStamp] <Warning>: A warning is logged
[TimeStamp] <Error>: An error has occurred

我想从文件中获取此文本,并根据尖括号中每种日志消息类型的复选框,用户可以隐藏他们不想看到的内容。例如取消选中“Step”复选框会隐藏 Step 行,但如果他们重新选中它,它将重新出现在文本窗口中。

我尝试将每一行存储到一个字符串中,该字符串存储在一个列表中以保持每一行的顺序,但是这种方法在更改文本时非常慢。方法如下图

logTextbox.Text = "";
foreach (string line in CompleteLog) //CompleteLog is list containing each line in log file
{
   if (CheckLine(line)) //Checks line based on what the user wants to see
   {
      WriteLine(line);
   }
}

任何建议都会非常受欢迎

编辑:

        private bool CheckLine(string line)
    {
        int left = line.IndexOf('<');
        int right = line.IndexOf(">:");

        string logtype = line.Substring(left+1, right - left-1);
        if (ValidLogs.Any(p => p.ToLower().Equals(logtype.ToLower())))
        {
            return true;
        }

        return false;
    }

ValidLogs 是一个字符串列表,其中包含允许在加载时设置的内容,并在与日志类型对应的复选框的检查事件中更改。上面的第一种方法用于加载和每个检查事件来更新显示的内容。

4

2 回答 2

0

Assuming you don't have memory limitations or excessively large files and your filter criteria are fixed you could pre-parse each line for the filter criteria and store the filter details on a log object you'd then have objects with simple properties to check rather having to re-parse everytime.

public class LogLine {
       public string Text { get; set; }
       public eLogLevel LogLevel { get; set; }
       // other filter properties
}

Then you would change your CheckLines() to filter the list of LogLine objects.

logTextbox.Text = "";
foreach (LogLine line in ParsedLog) //ParsedLog is list containing each line in log file pre-parsed
{
   if (CheckLine(line)) //Checks line based on what the user wants to see
   {
      WriteLine(line.Text);
   }
}
于 2013-02-22T01:03:22.867 回答
0

我认为实现这一点的最佳方法是使用 listview 控件。您可以在文本文件中每行添加一个项目,然后根据复选框过滤显示的项目(行)。我可以推荐ObjectListView作为实现这一目标并使其看起来更漂亮的一种方式。

于 2013-02-22T00:27:21.553 回答