en-US/about_PSJobLogger.help.txt
TOPIC about_PSJobLogger SHORT DESCRIPTION PSJobLogger provides a simple mechanism to log messages from parallel jobs LONG DESCRIPTION PSJobLogger is a wrapper around a set of ConcurrentDictionary and ConcurrentQueue objects which allow for access to logging facilities (e.g. Write-Output, Write-Debug) in thread-safe manner. It is implemented as a set of functions centered around a ConcurrentDictionary, and as a self-contained PowerShell class. Note that PowerShell classes are not thread-safe due to session affinity. PowerShell 7.4+ has a workaround for thread-safety of classes; the class version of PSJobLogger could be used safely in that environment. EXAMPLES Simple example using ForEach-Object -AsJob -Parallel with logger functions: Import-Module PSJobLogger -Force # Create a list of some 'data to process' $dataToProcess = @() # Initialize a new logger $jobLog = Initialize-PSJobLoggerDict -Name 'MyLogger' # Start parallel jobs $job = $dataToProcess | ForEach-Object -ThreadLimit 4 -AsJob -Parallel { Import-Module PSJobLogger -Force # If the module is not in your $PSModulePath $log = $using:jobLog Write-LogDebug -LogDict $log -Message "Starting to process $($_.Name)" Write-LogProgress -LogDict $log -Id $_.Id -ArgumentMap @{ Id = $_.Id; Activity = 'My Job Name'; Status = 'Processing'; PercentComplete = 0 } <# perform some data processing tasks here #> Write-LogVerbose -LogDict $log -Message "Finished processing $($_.Name)" Write-LogProgress -LogDict $log -Id $_.Id -ArgumentMap @{ Completed = $true } } # Monitor job state and show log messages while jobs are running while ($job.State -eq 'Running') { # write messages to the various output streams Show-Log -LogDict $jobLog # short sleep to not overload the UI Start-Sleep -Seconds 0.25 } # Show the job output and clean up finshed jobs Receive-Job -Job $job -Wait -AutoRemoveJob # Show any remaining messages Show-Log -LogDict $jobLog Example using ForEach-Object -AsJob -Parallel with a class: Import-Module PSJobLogger -Force # Create a list of some 'data to process' $dataToProcess = @() # Initialize a new logger $jobLog = Initialize-PSJobLogger -Name 'MyLogger' # Start parallel jobs $job = $dataToProcess | ForEach-Object -ThreadLimit 4 -AsJob -Parallel { Import-Module PSJobLogger -Force # If the module is not in your $PSModulePath $log = $using:jobLog $log.Debug("Starting to process $($_.Name)") $log.Progress($_.Id, @{ Id = $_.Id; Activity = 'My Job Name'; Status = 'Processing'; PercentComplete = 0 }) <# perform some data processing tasks here #> $log.Verbose("Finished processing $($_.Name)") $log.Progress($_.Id, @{ Completed = $true }) } # Monitor job state and show log messages while jobs are running while ($job.State -eq 'Running') { # write messages to the various output streams $jobLog.FlushStreams() # short sleep to not overload the UI Start-Sleep -Seconds 0.25 } # Show the job output and clean up finshed jobs Receive-Job -Job $job -Wait -AutoRemoveJob # Show any remaining messages $jobLog.FlushStreams() KEYWORDS job parallel foreach-object logger logging |