functions/internal/Write/Write-Log.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
function Write-Log { [Cmdletbinding()] param( [Parameter(Mandatory = $true)] [Serilog.Events.LogEventLevel]$LogLevel, [Parameter(Mandatory = $true)] [AllowEmptyString()] [AllowNull()] [string]$MessageTemplate, [Parameter(Mandatory = $false)] [Serilog.ILogger]$Logger = [Serilog.Log]::Logger, [Parameter(Mandatory = $false)] [AllowNull()] [System.Exception]$Exception, [Parameter(Mandatory = $false)] [AllowNull()] [System.Management.Automation.ErrorRecord]$ErrorRecord, [Parameter(Mandatory = $false)] [AllowNull()] [object[]]$PropertyValues, [Parameter(Mandatory = $false)] [switch]$PassThru ) # If ErrorRecord is available wrap it into RuntimeException if($null -ne $ErrorRecord){ if($null -eq $Exception){ $Exception = $ErrorRecord.Exception } $Exception = [PoShLog.Core.Exceptions.WrapperException]::new($Exception, $ErrorRecord) } switch ($LogLevel) { Verbose { $Logger.Verbose($Exception, $MessageTemplate, $PropertyValues) } Debug { $Logger.Debug($Exception, $MessageTemplate, $PropertyValues) } Information { $Logger.Information($Exception, $MessageTemplate, $PropertyValues) } Warning { $Logger.Warning($Exception, $MessageTemplate, $PropertyValues) } Error { $Logger.Error($Exception, $MessageTemplate, $PropertyValues) } Fatal { $Logger.Fatal($Exception, $MessageTemplate, $PropertyValues) } } if ($PassThru) { Get-FormattedMessage -Logger $Logger -LogLevel $LogLevel -MessageTemplate $MessageTemplate -PropertyValues $PropertyValues -Exception $Exception } } |