按日志行解析日志文件

Parse log file by log line

我正在使用 log4net,它偶尔会向日志文件写入异常。现在我想插入每个日志行,它可以超过 1 行(例如包括堆栈跟踪在内的异常),所以

foreach(var line in File.ReadAllLines(...)){}

会分解异常。这是一个示例日志文件:

2017-02-22 14:37:17,212 [Foo.Bar]  INFO  - This is something nice to log
System.InvalidCastException: Specified cast is not valid.
   at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
   at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
   at Foo.Bar.<>c__DisplayClass282_0.<FooBar>b__0(DataRow row) in somefile.cs:line 2657
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at Foo.Bar.<FooBar>d__282.MoveNext() in somefile.cs:line 2657

我希望在字符串中解析第一行,以及其余的所有内容(在本例中,通常是异常)。

有什么好的入门想法吗?

这是我想出的,使用正则表达式拆分行。但是,这是假设所有日志条目都以该格式的日期开头。

static void Main(string[] args)
{
    string RegexString = @"(?=\d{4}[-]\d{2}[-]\d{2}[ ]\d{2}[:]\d{2}[:]\d{2}[,])";
    string Log4NetSample = Properties.Resources.Log4Net_Example;
    List<string> ParsedLogItems = new List<string>();
    foreach (var Item in Regex.Split(Log4NetSample, RegexString))
        if (Item.Trim() != string.Empty)
            ParsedLogItems.Add(Item);


    foreach (var Item in ParsedLogItems)
    {
        Console.WriteLine();
        Console.WriteLine("===== New Log Item =====");
        Console.WriteLine(Item);
    }
    Console.ReadLine();
}

我将此文本用作演示,并更改了一些日期以表明它可以匹配多个日期。

2017-02-22 14:37:17,212 [Foo.Bar]  INFO  - This is something nice to log
System.InvalidCastException: Specified cast is not valid.
   at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
   at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
   at Foo.Bar.<>c__DisplayClass282_0.<FooBar>b__0(DataRow row) in somefile.cs:line 2657
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at Foo.Bar.<FooBar>d__282.MoveNext() in somefile.cs:line 2657

2012-03-31 14:37:17,212 [Foo.Bar]  INFO  - This is something nice to log
System.InvalidCastException: Specified cast is not valid.
   at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
   at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
   at Foo.Bar.<>c__DisplayClass282_0.<FooBar>b__0(DataRow row) in somefile.cs:line 2657
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at Foo.Bar.<FooBar>d__282.MoveNext() in somefile.cs:line 2657

2018-08-14 14:37:17,212 [Foo.Bar]  INFO  - This is something nice to log
System.InvalidCastException: Specified cast is not valid.
   at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
   at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
   at Foo.Bar.<>c__DisplayClass282_0.<FooBar>b__0(DataRow row) in somefile.cs:line 2657
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at Foo.Bar.<FooBar>d__282.MoveNext() in somefile.cs:line 2657

并且代码产生以下输出:

===== New Log Item =====
2017-02-22 14:37:17,212 [Foo.Bar]  INFO  - This is something nice to log
    System.InvalidCastException: Specified cast is not valid.
       at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
       at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
       at Foo.Bar.<>c__DisplayClass282_0.<FooBar>b__0(DataRow row) in somefile.cs:line 2657
       at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
       at Foo.Bar.<FooBar>d__282.MoveNext() in somefile.cs:line 2657


===== New Log Item =====
2012-03-31 14:37:17,212 [Foo.Bar]  INFO  - This is something nice to log
    System.InvalidCastException: Specified cast is not valid.
       at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
       at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
       at Foo.Bar.<>c__DisplayClass282_0.<FooBar>b__0(DataRow row) in somefile.cs:line 2657
       at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
       at Foo.Bar.<FooBar>d__282.MoveNext() in somefile.cs:line 2657


===== New Log Item =====
2018-08-14 14:37:17,212 [Foo.Bar]  INFO  - This is something nice to log
    System.InvalidCastException: Specified cast is not valid.
       at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
       at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
       at Foo.Bar.<>c__DisplayClass282_0.<FooBar>b__0(DataRow row) in somefile.cs:line 2657
       at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
       at Foo.Bar.<FooBar>d__282.MoveNext() in somefile.cs:line 2657