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

Skip to content
Tomáš Bouda edited this page Aug 8, 2021 · 5 revisions

Write-InformationLog

Writes Information log message

Write-InformationLog
    [-MessageTemplate] <string>
    [-Logger <ILogger>]
    [-Exception <Exception>]
    [-PropertyValues <Object[]>]
    [-PassThru]
    [<CommonParameters>]
Write-InformationLog
    [[-MessageTemplate] <string>]
    -ErrorRecord <ErrorRecord>
    [-Logger <ILogger>]
    [-Exception <Exception>]
    [-PropertyValues <Object[]>]
    [-PassThru]
    [<CommonParameters>]

Description

Write a log event with the Information level. Note that you can use Write-InfoLog as shortcut.

Examples

Example 1: Write simple log message
Write-InformationLog 'My Log Message'

Or use pipeline input

'My Log Message' | Write-InformationLog
Example 2: Write log message with properties
Write-InformationLog 'My {Number} Log Message.' -PropertyValues 'First'
# Alternatively
Write-InformationLog -MessageTemplate 'My {Number} Log Message.' -PropertyValues 'First'
Example 3: Write log message with ErrorRecord
try{
    throw 'error'
}
catch{
    Write-InformationLog -ErrorRecord $_
}

Or specify your own message

try{
    throw 'error'
}
catch{
    Write-InfoLog 'Error occurred while doing some business! {SomeNumber}' -ErrorRecord $_ -PropertyValues 123
}
Example 4: Write log message with Exception
try{
    throw 'error'
}
catch{
    Write-InformationLog 'Error occurred while doing some business!' -Exception $_.Exception
}
Example 5: Using -PassThru parameter

If for whatever reason you want to output a log message as string, you can use -PassThru parameter. Keep in mind that message will still be logged using all sinks.

$message = Write-InformationLog 'Some {What} that will be passed thru' -PropertyValues 'message' -PassThru
$message # Contains string: "Some message that will be passed thru"

You can alter ouputted message using Set-DefaultMessageFormatter cmdlet.

Set-DefaultMessageFormatter -Template '[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}'
$message = Write-InformationLog 'Some {What} that will be passed thru' -PropertyValues 'message' -PassThru
$message # Contains string: "[10:26:53 INF] Some message that will be passed thru"

Parameters

-MessageTemplate

Message template describing the event.

Type: string
Position: 0
Default value:
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
-Logger

Instance of Serilog.Logger. By default static property [Serilog.Log]::Logger is used. Use $logger = Start-Logger -PassThru to get instance of logger.

Type: Serilog.ILogger
Position: Named
Default value: [Serilog.Log]::Logger
Accept pipeline input: False
Accept wildcard characters: False
-Exception

Exception related to the event.

Type: System.Exception
Position: Named
Default value: $null
Accept pipeline input: False
Accept wildcard characters: False
-ErrorRecord

ErrorRecord related to the event.

Type: System.Management.Automation.ErrorRecord
Position: Named
Aliases: ER
Default value: $null
Accept pipeline input: False
Accept wildcard characters: False
-PropertyValues

Objects positionally formatted into the message template.

Type: object[]
Position: Named
Default value: $null
Accept pipeline input: False
Accept wildcard characters: False
-PassThru

If set outputs MessageTemplate populated with PropertyValues into pipeline.

Type: SwitchParameter
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
Clone this wiki locally