https://github.com/jcasale/psserilog
A PowerShell module for logging based on the Serilog library.
https://github.com/jcasale/psserilog
logging powershell powershell-modules
Last synced: about 1 month ago
JSON representation
A PowerShell module for logging based on the Serilog library.
- Host: GitHub
- URL: https://github.com/jcasale/psserilog
- Owner: jcasale
- License: mit
- Created: 2022-09-09T00:06:14.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2026-02-13T00:20:00.000Z (about 2 months ago)
- Last Synced: 2026-02-13T09:54:48.524Z (about 2 months ago)
- Topics: logging, powershell, powershell-modules
- Language: C#
- Homepage:
- Size: 109 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PowerShell Serilog Logging Module
The `PSSerilog` module provides logging based on the Serilog library.
## Installation
The module is distributed as a Windows Installer package (the PowerShell Gallery is not suitable for some enterprises).
Run the installer manually or in unattended mode:
```bat
msiexec.exe /i ps-serilog.msi /qn
```
The default installation path is:
```bat
%ProgramFiles%\WindowsPowerShell\Modules\PSSerilog
```
## Documentation
Use `Get-Command` and `Get-Help` to enumerate the cmdlets with this module and obtain their documentation:
```powershell
Get-Command -Module PSSerilog
Get-Help New-SerilogLoggerConfiguration -Full
```
## Examples
- Create a basic logger using a sane pattern:
```powershell
try
{
$name = [IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name)
$path = [IO.Path]::ChangeExtension($MyInvocation.MyCommand.Path, '.log')
$logger = New-SerilogBasicLogger -Name $name -Path $path -ErrorAction Stop
}
catch
{
throw
}
function main
{
[CmdletBinding()]
param()
$logger.Information('Executing script...')
# Your code follows.
}
try
{
main -ErrorAction Stop
}
catch
{
$logger.Fatal($_.Exception, 'Execution failed.')
throw
}
finally
{
# Call Dispose() here and not Close-SerilogDefaultLogger as the instance was not applied to the static logger.
$logger.Dispose()
}
```
- Create a basic logger and apply it to the Serilog default static logger:
```powershell
$name = [IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name)
$path = [IO.Path]::ChangeExtension($MyInvocation.MyCommand.Path, '.log')
$logger = New-SerilogBasicLogger -Name $name -Path $path -ErrorAction Stop |
Set-SerilogDefaultLogger -ErrorAction Stop
try
{
# The other-script.ps1 can call Get-SerilogDefaultLogger to get a logger configured however necessary.
& "$PSScriptRoot\other-script.ps1"
}
catch
{
$logger.Fatal($_.Exception, 'Execution failed.')
throw
}
finally
{
Close-SerilogDefaultLogger
}
```
> **Warning**
> Don't call `Set-SerilogDefaultLogger` more than once without calling `Close-SerilogDefaultLogger`.
- Create a custom logger and a global context:
```powershell
$template = '[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{Level}] [{MyValue}] {Message:l}{NewLine}{Exception}'
$configuration = New-SerilogLoggerConfiguration -MinimumLevel Verbose -Properties @{MyValue=42} |
Add-SerilogSinkConsole -OutputTemplate $template
$logger = New-SerilogLogger -Configuration $configuration
$logger.Information('Message 1')
```
Results in:
```text
[2023-05-28 18:27:07.489] [Information] [42] Message 1
```