Last active
July 29, 2021 18:14
-
-
Save dsudduth/fd66ef6057c701e9ece338df1a8ab019 to your computer and use it in GitHub Desktop.
An example of structured logging for PowerShell scripts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .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