@@ -43,61 +43,56 @@ function Read-ChocoLog {
4343 [string ]
4444 $PatternLayout = ' %date %thread [%-5level] - %message'
4545 )
46-
4746 $files = Get-Item - Path $Path
4847 if ($files.PSIsContainer ) {
49- $files = Get-ChildItem - Path $Path - Filter $Filter | Sort-Object - Property LastWriteTime | Select-Object - Last $FileLimit
48+ $files = Get-ChildItem - Path $Path - Filter $Filter |
49+ Sort-Object - Property LastWriteTime | Select-Object - Last $FileLimit
5050 }
51+ Write-Verbose " Found files: $ ( $files -join ' ,' ) "
5152
52- [System.Collections.Generic.List [ChocoLog ]]$parsed = @ ()
53- $detected = [System.Collections.Generic.List [int ]]::new()
54-
55- $RegularExpression = Convert-PatternLayout $PatternLayout
53+ $parsed = @ {}
5654
55+ # Get the regex for the Log4Net PatternLayout
56+ $RegularExpression = Convert-PatternLayout - PatternLayout $PatternLayout
5757 $files | ForEach-Object - Process {
5858 $file = $_
59+ Write-Verbose " Reading over file: $file "
5960 $raw = [System.IO.File ]::ReadAllLines($file.FullName )
60-
61+ Write-Verbose " Lines read: $ ( $raw .Count ) "
6162 # Iterate over each line
6263 foreach ($line in $raw ) {
63- # Write-Debug $line
64+ Write-Debug $line
6465 $m = $RegularExpression.match ($line )
6566 if ($m.Success ) {
66- $threadMatch = $m.Groups [' thread' ].Value
67- # If it matches the regex, tag it
68- if ($threadMatch -ne $currentSession.thread ) {
69- # This is a different thread
70-
71- # Save the current session to the parsed list
72- if ($currentSession ) {
73- $parsed.Add ($currentSession ) > $null
74- }
75-
76- # Look up if current thread exists in Parsed and append to that if so
77- if ($detected.Contains ($threadMatch )) {
78- $currentSession = $parsed | Where-Object { $_.Thread -eq $threadMatch }
79- } else {
80- # We haven't seen this thread before, let's make a new object
81- $detected.Add ($threadMatch ) > $null
82- $currentSession = [ChocoLog ]::new(
67+ [int ]$threadMatch = $m.Groups [' thread' ].Value
68+ # Replace comma with period to make it a valid datetime
69+ [datetime ]$currentDateTime = $m.Groups [' date' ].Value -replace ' ,' , ' .'
70+ # Check if thread exists, if not make it.
71+ if (-not ($parsed.ContainsKey ($threadMatch ))) {
72+ Write-Verbose " New thread detected: $threadMatch "
73+ $null = $parsed.Add (
74+ $threadMatch ,
75+ [ChocoLog ]::new(
8376 $threadMatch ,
84- ($m.Groups [' date' ].Value -replace ' ,' , ' .' ),
8577 $file
8678 )
87- }
79+ )
8880 }
89-
90- $currentSession .logs.Add (
81+ Write-Verbose " Adding new log line to thread $threadMatch "
82+ $parsed .Item ( $threadMatch ).AddLogLine (
9183 [Log4NetLogLine ]::new(
92- [ Datetime ]( $m .Groups [ ' date ' ].Value -replace ' , ' , ' . ' ) ,
84+ $currentDateTime ,
9385 $threadMatch ,
9486 $m.Groups [' level' ].Value,
9587 $m.Groups [' message' ].Value
96- )) > $null
88+ ))
9789 } else {
90+ Write-Verbose " Line did not match regex"
91+ Write-Debug $line
9892 # if it doesn't match regex, append to the previous
99- if ($currentSession ) {
100- $currentSession.logs [-1 ].AppendMessage($line )
93+ if ($threadMatch ) {
94+ Write-Verbose " Appending to existing thread: $threadMatch "
95+ $parsed.Item ($threadMatch ).AppendLastLogLine($line )
10196 } else {
10297 # This might happen if the log starts on what should have been a
10398 # multiline entry... Not very likely
@@ -106,17 +101,15 @@ function Read-ChocoLog {
106101 }
107102 }
108103 }
109- # Write out the last log line!
110- if (-Not $parsed.Contains ($currentSession )) {
111- $parsed.Add ($currentSession ) > $null
112- }
113104
114105 # Doing this at the end since threads can get mixed
115- $parsed | ForEach-Object {
106+ $parsed.Keys | ForEach-Object {
107+ Write-Verbose " Parsing special logs for: $_ "
116108 # This updates fields like: cli, environment, and configuration
117- $_ .ParseSpecialLogs ()
109+ $parsed .Item ( $_ ) .ParseSpecialLogs()
118110 }
119111
120112 # Return the whole parsed object
121- $parsed
113+ Write-Verbose " Returning results in desceding order. Count: $ ( $parsed.Count ) "
114+ $parsed.Values | Sort-Object - Descending Time
122115}
0 commit comments