Skip to content

Instantly share code, notes, and snippets.

@dsudduth
Last active July 29, 2021 18:14
Show Gist options
  • Select an option

  • Save dsudduth/fd66ef6057c701e9ece338df1a8ab019 to your computer and use it in GitHub Desktop.

Select an option

Save dsudduth/fd66ef6057c701e9ece338df1a8ab019 to your computer and use it in GitHub Desktop.
An example of structured logging for PowerShell scripts.
<#
.SYNOPSIS
Demonstrates structured logging in PowerShell.
.DESCRIPTION
The PoShLog module provides a simple wrapper around Serilog; a well-known logger for .Net applications.
For more details, see https://github.com/PoShLog/PoShLog/wiki/Getting-started.
.NOTES
Version: 1.0.0
Author: Derek Sudduth
Creation Date: 2021-07-06
Purpose/Change: Initial creation.
#>
# Initialization
Install-Module -Name PoShLog
New-Logger |
Set-MinimumLevel -Value Verbose |
Add-SinkFile -Path 'example.log' -Formatter (Get-JsonFormatter) -OutputTemplate '{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}' |
Add-SinkConsole |
Start-Logger
# Script Contents
Write-InfoLog 'This is a info message'
Write-DebugLog 'This is a debug message'
Write-WarningLog 'This is a warning message'
Write-ErrorLog 'This is a error message'
# Close the logger
Close-Logger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment