我正在使用log4net
,它偶尔会在日志文件中写入异常。现在我想插入每个日志行,这可能超过1行(例如包括stacktrace在内的异常),所以
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
我想要在字符串中解析第一行,以及剩余的剩余部分(在此示例中,通常是异常)。
有什么好主意可以开始吗?
答案 0 :(得分:0)
这是我提出的,使用Regex分割行。但是,这假设所有日志条目都以该格式的日期开头。
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