Skip to content

A PowerShell module for logging based on the Serilog library.

License

Notifications You must be signed in to change notification settings

jcasale/PSSerilog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

msiexec.exe /i ps-serilog.msi /qn

The default installation path is:

%ProgramFiles%\WindowsPowerShell\Modules\PSSerilog

Documentation

Use Get-Command and Get-Help to enumerate the cmdlets with this module and obtain their documentation:

Get-Command -Module PSSerilog
Get-Help New-SerilogLoggerConfiguration -Full

Examples

  • Create a basic logger using a sane pattern:

    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:

    $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:

    $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:

    [2023-05-28 18:27:07.489] [Information] [42] Message 1