In the following idea, I think the syntax | & $LogIt is a little messy. It is (IMO) certainly better than having tons of | Out-File -FilePath thelog.log -Append sprinkled in a script. I find that it is WAY too easy to cut-paste and forget to change the logfile name. Even worse is when forgetting to "initialize" (not -Append) a log once.
Am I missing another PowerShell concept that would help accomplish the idea behind this closure?
function MakeLogFile([string]$filename)
{
Get-Date | Out-File -FilePath $filename
{
param(
[ Parameter(ValueFromPipeline=$true,Mandatory=$true) ]
[string] $in
)
process {
$in | Out-File -FilePath $filename -Append
}
}.GetNewClosure()
}
$LogIt = MakeLogFile thelog.log
&$LogIt "monkey"
"1" | & $LogIt
"2" | & $LogIt
This sample doesn't show some other "features" in MakeLogFile to focus my question.