From db1503fc3b2b074a83dba4f4c01015292c3a24d7 Mon Sep 17 00:00:00 2001 From: Steve Dinn Date: Thu, 7 Dec 2023 01:06:09 +0000 Subject: [PATCH] Consolidate StringBuilder method calls. Remove commented code. --- source/CaddyLog-To-AccessLog.ps1 | 60 ++++++++++++-------------------- 1 file changed, 23 insertions(+), 37 deletions(-) diff --git a/source/CaddyLog-To-AccessLog.ps1 b/source/CaddyLog-To-AccessLog.ps1 index a720967..7d5f716 100644 --- a/source/CaddyLog-To-AccessLog.ps1 +++ b/source/CaddyLog-To-AccessLog.ps1 @@ -25,46 +25,32 @@ Get-Content -Tail $lines -Wait -Path $inFile | ForEach-Object { $userAgent = $r.headers.'User-Agent' | Select-Object -First 1 $referer = $r.headers.Referer | Select-Object -First 1 - $null = $builder.Clear() - $null = $builder.Append($r.remote_ip) - $null = $builder.Append(" - [") - $null = $builder.Append($baseDate.AddSeconds($l.ts).ToString("yyyy-MM-dd HH:mm:ss.fff")) - $null = $builder.Append("] """) - $null = $builder.Append($r.host) - $null = $builder.Append(""" ") - $null = $builder.Append($r.method) - $null = $builder.Append(" ") - $null = $builder.Append($r.uri) - $null = $builder.Append(" ") - $null = $builder.Append($r.proto) - $null = $builder.Append(" ") - $null = $builder.Append($l.status) - $null = $builder.Append(" ") - $null = $builder.Append($l.size) - $null = $builder.Append(" """) - $null = $builder.Append($referer) - $null = $builder.Append(""" """) - $null = $builder.Append($userAgent) - $null = $builder.Append("""") - - $commonLog = $builder.ToString() + $commonLog = $builder + .Clear() + .Append($r.remote_ip) + .Append(" - [") + .Append($baseDate.AddSeconds($l.ts).ToString("yyyy-MM-dd HH:mm:ss.fff")) + .Append("] """) + .Append($r.host) + .Append(""" ") + .Append($r.method) + .Append(" ") + .Append($r.uri) + .Append(" ") + .Append($r.proto) + .Append(" ") + .Append($l.status) + .Append(" ") + .Append($l.size) + .Append(" """) + .Append($referer) + .Append(""" """) + .Append($userAgent) + .Append("""") + .ToString() Write-Output $commonLog $outputWriter.WriteLine($commonLog) $outputWriter.Flush() - - #[system.string]::Join( - # " ", - # @( - # "$($r.remote_ip) - [$($baseDate.AddSeconds($l.ts).ToString("yyyy-MM-dd HH:mm:ss.fff"))]", - # """$($r.host)""", - # $r.method, - # $r.uri, - # $r.proto, - # $l.status, - # $l.size, - # """$($r.headers.Referer)""", - # """$ua""" - # )) }