0

我有一个要在 DataGridView 中显示的日志文件。

文件中的示例数据行是:-

<![LOG[Creating mandatory request for advert 0002124C, program Shutdown Desktops Overnight, package 0000073C]LOG]!><time="05:00:00.192+000" date="02-11-2013" component="execmgr" context="" type="1" thread="3712" file="execreqmgr.cpp:2858">

我想提取上面示例的各个方面,日志描述、时间和日期、组件、上下文、类型和线程......并将它们作为列添加到 DataGridView 的数据源中。

解决提取这些数据的最佳方法是什么?

4

1 回答 1

0

我会建议 DJ Kraze 的方法:创建一个自定义列表并将其绑定到您的 DataGridView。只需使用自定义代码来解析该行。如果必须,您也可以使用 DataTable,但是 List 方法通常更简洁。

类似的东西(我没有检查确切的语法或方法调用,因此仅用作示例,例如,您将需要一个文本阅读器):

public class LogEntry {
   public string Description { get; set; }
   public DateTime LogDate { get; set; }
   // other properties you want to extract
}

public class LogReader {
   public List<LogEntry> ReadLog(string fileName){
       var parsedLog = new List<LogEntry>();
       using(var file = File.Open(filename, ....)){
           while(var line = file.ReadLine()){
               var logEntry = ParseLine(line);
               parsedLog.Add(logEntry);
           }
       }
       return parsedLog;
   }

   private LogEntry ParseLine(string line){
       var logEntry = new LogEntry();
       // go through the line and parse it with string functions and populate values.
       // I use a helper class, a shortened version is below - note, you might need to
      //adjust to compile
       var parser = new StringParser(line);
       //now just use GetBetween to find the values you need. just pick your delimiters
       // carefully as the parser will move beyond the end string.  But it will go
       // sequentially so just experiment
      logEntry.Description = parser.GetBetween("LOG[", "]LOG");
      // go through in order
   } 
}

public class StringParser {
    private string text;
    private int position;

    public StringParser(string text)
    {
        this.Text = text;
    }  

    public string Text
    {
        get { return this.text; }
        private set
        {
            this.text = value;
            Position = 0;
        }
    }

    public int Position
    {
        get { return this.position; }
        private set
        {
            if (value < 0)
            {
                this.position = 0;
            }
            else
            {
                this.position = value > this.Text.Length ? this.Text.Length : value;
            }
        }
    }

    public bool AtEnd
    {
        get { return this.Position >= this.Text.Length; }
    }

    public string GetBetween(string beforeText, string afterText)
    {
        var startPos = MoveAfter(beforeText);
        if (startPos == -1)
        {
            return "";
        }

        var endPos = FindNext(afterText);
        return GetBetween(startPos, endPos);
    }

     public string PeekBetween(int startPos, int endPos)
    {
        if (startPos < 0 || startPos >= this.text.Length)
        {
            return "";
        } 
        if (endPos < 0 || endPos > this.text.Length)
        {
            endPos = this.text.Length;
        } 
        if (startPos >= endPos)
        {
            return "";
        }

        var result = this.text.Substring(startPos, endPos - startPos);
        return result;
    }

    public string GetBetween(int startPos, int endPos)
    {
        if (endPos < 0 || endPos > this.text.Length)
        {
            endPos = this.text.Length;
        } 
        var result = PeekBetween(startPos, endPos);
        if (!string.IsNullOrEmpty(result))
        {
            this.Position = endPos;
        }
        return result;
    }


    public int FindNext(string searchText)
    {
        if (string.IsNullOrEmpty(searchText) || this.AtEnd)
        {
            return -1;
        } 
        return this.text.IndexOf(searchText, this.Position, StringComparison.Ordinal);
    }   

    public int MoveAfter(string searchText)
    {
        var found = FindNext(searchText);
        if (found > -1)
        {
            found += searchText.Length;
            this.Position = found;
        }
        return found;
    } 
}
于 2013-02-19T14:54:42.283 回答