Skip to content

Latest commit

 

History

97 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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
    {
        $path = [IO.Path]::ChangeExtension($MyInvocation.MyCommand.Path, '.log')
        $logger = New-SerilogBasicLogger -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, named root logger and apply that root logger to the Serilog default static logger:

    $name = [IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name)
    $path = [IO.Path]::ChangeExtension($MyInvocation.MyCommand.Path, '.log')
    $logger = New-SerilogBasicLogger -Path $path -SourceContext $name -ErrorAction Stop |
        Set-SerilogDefaultLogger
    
    try
    {
        # The other-script.ps1 can call Get-SerilogDefaultLogger to get the
        # root logger, and either use it, or create additional named loggers.
        & "$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
    

About

A PowerShell module for logging based on the Serilog library.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages