Skip to content

romandres/PSStreamLogger

Repository files navigation

PSStreamLogger

This module allows you to use the built-in functionality of PowerShell streams to log data and output it into multiple log targets.

Use the PowerShell Write-* cmdlets to write into PowerShell streams in your scripts and modules like this:

Write-Error "Error message"
Write-Warning "Warning message"
Write-Information -MessageData "Information message"
Write-Host "Information message" # Write-Host writes into the information stream
Write-Debug "Debug message"
Write-Verbose "Verbose message"

And use the PSStreamLogger to log this data into log files or other log targets.

Scripts and modules that already write into PowerShell streams do not have to be updated and can just be invoked through the PSStreamLogger.

Installation

Install-Module PSStreamLogger

Usage

Configure the log module and invoke a script inside a wrapper script:

Import-Module PSStreamLogger

# Create plain text file logger
$fileLogger = New-FileLogger -FilePath "C:\temp\script1.log" -MinimumLogLevel Verbose

# Execute script with logger
Invoke-CommandWithLogging -ScriptBlock { & "C:\temp\script1.ps1" -Verbose -InformationAction Continue } -Loggers @($fileLogger)

You can also directly execute commands in the script block without calling an external script:

...

# Execute commands with logger
Invoke-CommandWithLogging -ScriptBlock {
    Write-Verbose "Creating file" -Verbose
    New-Item -Path "C:\temp\file1.txt" -Type File
} -Loggers @($fileLogger)

Configure the log module and invoke a script in one line in PowerShell:

Import-Module PSStreamLogger; Invoke-CommandWithLogging -ScriptBlock { & 'C:\temp\script1.ps1' -Verbose -InformationAction Continue } -Loggers @(New-FileLogger -FilePath 'C:\temp\script1.log' -MinimumLogLevel Verbose)

Configure the log module and invoke a script in one command-line command:

powershell.exe -Command "& { Import-Module PSStreamLogger; Invoke-CommandWithLogging -ScriptBlock { & 'C:\temp\script1.ps1' -Verbose -InformationAction Continue } -Loggers @(New-FileLogger -FilePath 'C:\temp\script1.log' -MinimumLogLevel Verbose) }"

Targets

This module allows you to log to:

  • Console output
  • Plain text files
  • Windows EventLog (only on Windows)

Credits

This module relies on Serilog and Microsoft.Extensions.Logging to be able to log the PowerShell streams into multiple targets.

About

PowerShell module to leverage data written into PowerShell streams for logging purposes.

Topics

Resources

License

Stars

Watchers

Forks