0

我有一个示例日志,我正在尝试找到获取信息的最佳解决方案:主要time stamp是一个特定的字符串。

示例日志输出:每个都在 1 行

####<Jun 22, 2012 12:54:18 PM CDT> <Notice> <WebLogicServer> <lname> <dname> <main>
<<WLS Kernel>> <> <BEA-000360> <Application started in RUNNING mode>

每个日志行都有 4 个开始#标记,后跟<Timestamp>与我的场景相关的所有片段都有<>括起来的括号,我想要一个信息数组,其中索引基于从左到右的读取。

对于每个<>括号,我想将其信息存储在 lineArray 中。

using (var reader = new StreamReader(@"C:\Projects\test.txt"))
{
    foreach(var line in ReadLines(reader)) 
    {
          //add logic to parse and read info into brackets
        String sLine = (String)line;
        String splitD = sLine.Split("<>")[0];
    }
}
4

1 回答 1

3

好吧,我不是正则表达式专家,但这应该可行:

string test = "####<Jun 22, 2012 12:54:18 PM CDT> <Notice> <WebLogicServer> <lname> <dname> <main>"
    + "<<WLS Kernel>> <> <BEA-000360> <Application started in RUNNING mode>";
var pattern = @"\<+(.*?)\>+";
var matches = Regex.Matches(test, pattern);
foreach (Match m in matches)
{
    Console.WriteLine(string.Format("-{0}-", m.Groups[1]));
}

Console.ReadKey();

-输出(字符之间的实际匹配):

-Jun 22, 2012 12:54:18 PM CDT-
-Notice-
-WebLogicServer-
-lname-
-dname-
-main-
-WLS Kernel-
--
-BEA-000360-
-Application started in RUNNING mode-

string test = "####<Jun 22, 2012 12:54:18 PM CDT> <Notice> <WebLogicServer> <lname> <dname> <main> <<WLS Kernel>> <> <BEA-000360> <Application started in RUNNING mode>"
    + Environment.NewLine + "####<Jun 23, 2012 12:54:18 PM CDT> <Notice> <WebLogicServer> <lname> <dname> <main> <<WLS Kernel>> <> <BEA-000360> <Application started in RUNNING mode>"
    + Environment.NewLine + "####<Jun 24, 2012 12:54:18 PM CDT> <Notice> <WebLogicServer> <lname> <dname> <main> <<WLS Kernel>> <> <BEA-000360> <Application started in RUNNING mode>";

List<Foo> foo = new List<Foo>();
using (StringReader reader = new StringReader(test))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        string pattern = @"\<+(.*?)\>+";
        var matches = Regex.Matches(line, pattern);

        foo.Add(new Foo
            {
                Timestamp = matches[0].Groups[1].ToString(),
                Field2 = matches[1].Groups[1].ToString()
            });
    }
}

输出:

3 个 Foo 对象的列表,每个对象都有不同的时间戳(6 月 22 日、6 月 23 日、6 月 24 日)

于 2012-08-08T17:27:46.690 回答