Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Tomáš Bouda edited this page Jun 28, 2021 · 16 revisions

Quick info

In order to log, you would want to set minimum level, below which no events should be generated, add locations(sinks) where to write event messages, eventually add message enrichements.

Sinks

First thing, you need to understand are sinks. Sinks are basically locations where we want to log our log messages. So for example, we have file sink, that logs into file, console sink that outputs into console host and so on. Here are supported sinks. You can see all Serilog(C# library, PoShLog is built upon) sinks here, but keep in mind, that at this moment PoShLog supports just few of them. If you would like to implement sink into PoShLog, please raise an issue.

Starting logger

In order to log your messages into desired sinks, you need to create logger first. You have two options and selecting one of them depends on what you are trying to do and how much customization you need.

Short version

To create and start logger quickly you can use Start-Logger cmdlet. It allows you to choose between Console and File sinks only. Keep in mind that you can alter just few settings: -MinimumLevel, -FilePath and -FileRollingInterval. If you need more customization skip to Full version.

Example 1: Start-Logger - log into file C:\Data\my_awesome.log and console
Import-Module PoShLog

# Create and start new logger
Start-Logger -FilePath 'C:\Data\my_awesome.log' -Console

Write-InfoLog 'Hurrray, my first log message'
Write-ErrorLog 'Oops, error occurred!'

# Don't forget to close the logger
Close-Logger

usage_gif

Full version

In order to get maximum functionality from PoShLog, start configuring logger with New-Logger cmdlet. This allows you to setup logger using pipeline - very similar approach to C# fluent API. You can add as many sinks as you want, you can add enrichements and set initial minimum level. Checkout these cmdlets to learn more information: New-Logger, Add-SinkFile, Add-SinkConsole, Start-Logger.

Example 2: Log into file C:\Data\my_awesome-*.log with rolling(every day new file) and console, both at minimum level Verbose
Import-Module PoShLog

# Create new logger
New-Logger | # Create new instance of logger configuration
    Set-MinimumLevel -Value Verbose | # Set minimum level, below which no events should be generated
    Add-SinkFile -Path 'C:\Data\my_awesome-.log' -RollingInterval Day | # Add sink that will write our event messages into file
    Add-SinkConsole | # Add sink that will log our event messages into console host
    Start-Logger # Start logging

# Write few messages to test functionality
Write-VerboseLog 'Test verbose message'
Write-DebugLog 'Test debug message'

# Close logger to release all resources, like `C:\Data\my_awesome-*.log` file
Close-Logger

Output templates

Text-based sinks use output templates to control message formatting. This formatting can be modified through the -OutputTemplate parameter:

Add-SinkFile -Path 'log.txt' -OutputTemplate '{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}'

In example above you can see default template, using buil-in properties like Level and Timestamp. You can add more properties using enrichers. Add {Properties:j} to the output template to include additional context information. See Serilog documentation.

Minimum level

Minimum level sets the log event level, bellow which no events will be generated.

Example 3: Setting minimum level to Debug

In this example, we set minimum level to Debug, that means log event messages with level Debug and higher(Information, Warning, Error and Fatal) will be processed and in this case written to console. Messages with lower levels - Verbose(Write-VerboseLog cmdlet) will be ignored. Default minimum level is Information.

New-Logger |
    Set-MinimumLevel -Value Debug |
    Add-SinkConsole |
    Start-Logger

See all log event levels here.

Overriding minimum level per sink

Every provided sink supports -RestrictedToMinimumLevel parameter. This parameter is useful in cases you want to write detailed logs in one location but less detailed in another. Keep in mind that minimum level for sink can only be raised. Lets see example.

Example 4: Overriding minimum log level for Console sink

In this example we can see, that debug and higher messages will be written into file, while Information and higher level messages will be written to the console.

New-Logger |
    Set-MinimumLevel -Value Debug |
    Add-SinkFile -Path 'log.txt' |
    Add-SinkConsole -RestrictedToMinimumLevel Information  |
    Start-Logger

Write messages

Logging is all about writing event messages. PoShLog offers 6 levels of messages: Verbose, Debug, Information, Warning, Error, Fatal. That's 6 cmdlets: Write-VerboseLog, Write-DebugLog, Write-InfoLog, Write-WarningLog, Write-ErrorLog, Write-FatalLog. It's up to you to decide where to place them in your code and what level to choose. You can decide later which messages are generated or not, dependant on their level using Set-MinimumLevel or Set-LevelSwitch.

Example 5: Write all levels of messages into console
Import-Module PoShLog

Start-Logger # Short version

# Test all log levels
Write-VerboseLog 'Test verbose message'
Write-DebugLog 'Test debug message'
Write-InfoLog 'Test info message'
Write-WarningLog 'Test warning message'
Write-ErrorLog 'Test error message'
Write-FatalLog 'Test fatal message'

Close-Logger

Close logger

Don't forget to close logger at the end of every session. This releases all resources safely.

Clone this wiki locally