使用日志(多行)和正则表达式
本文关键字:正则表达式 多行 日志 | 更新日期: 2023-09-27 17:50:49
具有以下日志:
2014-02-13 16:06:52,226 [8] ERROR solucao.projeto.arquivo - AutenticarUsuario
System.Threading.ThreadAbortException: O thread estava sendo anulado.
em System.Threading.Thread.AbortInternal()
em System.Threading.Thread.Abort(Object stateInfo)
em System.Web.HttpResponse.End()
2014-02-13 16:06:52,226 [8] ERROR solucao.projeto.arquivo - AutenticarUsuario
System.Threading.ThreadAbortException: O thread estava sendo anulado.
em System.Threading.Thread.AbortInternal()
em System.Threading.Thread.Abort(Object stateInfo)
em System.Web.HttpResponse.End()
尝试使用regex表达式恢复,我无法检索属于异常的全文。
(?<data>'d{4}'-'d{2}'-'d{2}'s'd{2}':'d{2}':'d{2}','d{3})'s'[(?<thread>[0-9]+)']'s(?<nivel>[A-Z]+)'s(?<classe>([A-Za-z]+'.)+[A-Za-z]+)'s'-'s(?<metodo>[A-Za-z_]+)'n(?<message>.+)
如何恢复完整的异常?
不确定您的单个捕获,但像这样的东西应该可以工作
# @"(?ms)^(?<data>[^'[']'r'n]+)[ 't]+'[(?<thread>[0-9]+)'][ 't]+(?<nivel>[A-Z]+)[ 't]+(?<classe>(?:[A-Za-z]+'.)+[A-Za-z]+)[ 't]+-[ 't]+(?<message>(?:(?!^[^'[']'r'n]+[ 't]+'[[0-9]+']).)+)"
(?ms) # Multi-line, Dot-All
^ # Beginning of line
(?<data> [^'[']'r'n]+ ) # (1), Date/time
[ 't]+ # Horizontal whitespace ( could use ''h' instead )
'[ # '['
(?<thread> [0-9]+ ) # (2), Thread number
'] # ']'
[ 't]+ # Horizontal whitespace
(?<nivel> [A-Z]+ ) # (3), Error
[ 't]+ # Horizontal whitespace
(?<classe> # (4 start), Class
(?: [A-Za-z]+ '. )+
[A-Za-z]+
) # (4 end)
[ 't]+ # Horizontal whitespace
- # '-'
[ 't]+ # Horizontal whitespace
(?<message> # (5 start), Message
(?: # Cluster group
(?! # Lookahead
^ # Not the start of a new exception
[^'[']'r'n]+ # ( basically, not the above code )
[ 't]+
'[
[0-9]+
']
) # End Lookahead
. # Continue to grab characters
)+ # End Cluster group, do 1 or more times
) # (5 end)
edit(删除Perl测试用例。添加c#测试用例)
c#测试用例string sLog =
@"
2014-02-13 16:06:52,226 [8] ERROR solucao.projeto.arquivo - AutenticarUsuario
System.Threading.ThreadAbortException: O thread estava sendo anulado.
em System.Threading.Thread.AbortInternal()
em System.Threading.Thread.Abort(Object stateInfo)
em System.Web.HttpResponse.End()
2014-02-13 16:06:52,226 [8] ERROR solucao.projeto.arquivo - AutenticarUsuario
System.Threading.ThreadAbortException: O thread estava sendo anulado.
em System.Threading.Thread.AbortInternal()
em System.Threading.Thread.Abort(Object stateInfo)
em System.Web.HttpResponse.End()
";
Regex Rx = new Regex(
@"
(?ms) # Multi-line, Dot-All
^ # Beginning of line
(?<data> [^'[']'r'n]+ ) # (1), Date/time
[ 't]+ # Horizontal whitespace ( could use ''h' instead )
'[ # '['
(?<thread> [0-9]+ ) # (2), Thread number
'] # ']'
[ 't]+ # Horizontal whitespace
(?<nivel> [A-Z]+ ) # (3), Error
[ 't]+ # Horizontal whitespace
(?<classe> # (4 start), Class
(?: [A-Za-z]+ '. )+
[A-Za-z]+
) # (4 end)
[ 't]+ # Horizontal whitespace
- # '-'
[ 't]+ # Horizontal whitespace
(?<message> # (5 start), Message
(?: # Cluster group
(?! # Lookahead
^ # Not the start of a new exception
[^'[']'r'n]+ # ( basically, not the above code )
[ 't]+
'[
[0-9]+
']
) # End Lookahead
. # Continue to grab characters
)+ # End Cluster group, do 1 or more times
) # (5 end)
", RegexOptions.IgnorePatternWhitespace);
Match matchX = Rx.Match( sLog );
while ( matchX.Success )
{
Console.WriteLine( "----------------------" );
Console.WriteLine( "New Exception" );
Console.WriteLine( "Date:'t" + matchX.Groups[1] );
Console.WriteLine( "Thread:'t" + matchX.Groups[2] );
Console.WriteLine( "Err:'t" + matchX.Groups[3] );
Console.WriteLine( "Class:'t" + matchX.Groups[4] ) ;
Console.WriteLine( "Msg --" );
Console.WriteLine( matchX.Groups[5] );
matchX = matchX.NextMatch();
}
输出>>
----------------------
New Exception
Date: 2014-02-13 16:06:52,226
Thread: 8
Err: ERROR
Class: solucao.projeto.arquivo
Msg --
AutenticarUsuario
System.Threading.ThreadAbortException: O thread estava sendo anulado.
em System.Threading.Thread.AbortInternal()
em System.Threading.Thread.Abort(Object stateInfo)
em System.Web.HttpResponse.End()
----------------------
New Exception
Date: 2014-02-13 16:06:52,226
Thread: 8
Err: ERROR
Class: solucao.projeto.arquivo
Msg --
AutenticarUsuario
System.Threading.ThreadAbortException: O thread estava sendo anulado.
em System.Threading.Thread.AbortInternal()
em System.Threading.Thread.Abort(Object stateInfo)
em System.Web.HttpResponse.End()
Press any key to continue . . .