diff --git a/src/internal/functions/Write-AzOpsMessage.ps1 b/src/internal/functions/Write-AzOpsMessage.ps1 index 11554c4d..ca6e121c 100644 --- a/src/internal/functions/Write-AzOpsMessage.ps1 +++ b/src/internal/functions/Write-AzOpsMessage.ps1 @@ -30,10 +30,13 @@ [CmdletBinding()] param ( + [Parameter(Mandatory = $false)] [bool] $ApplicationInsights = (Get-PSFConfigValue -FullName 'AzOps.Core.ApplicationInsights'), + [Parameter(Mandatory = $false)] [hashtable] $Data, + [Parameter(Mandatory = $false)] [System.Management.Automation.ErrorRecord] $ErrorRecord, [Parameter(Mandatory = $true)] @@ -43,16 +46,22 @@ [Parameter(Mandatory = $true)] [string] $LogString, + [Parameter(Mandatory = $false)] [string[]] $LogStringValues, + [Parameter(Mandatory = $false)] [int] $Metric, + [Parameter(Mandatory = $false)] [string] $MetricName, + [Parameter(Mandatory = $false)] [string] $FunctionName, + [Parameter(Mandatory = $false)] [string] $ModuleName, + [Parameter(Mandatory = $false)] [string] $Target ) @@ -67,56 +76,22 @@ } process { # Evaluate message verbosity - switch ($LogLevel) { - "Warning" { - [int]$intLevel = "666" - break - } - "Error" { - [int]$intLevel = "667" - break - } - "Critical" { - [int]$intLevel = "1" - break - } - { ($_ -eq "Important") -or ($_ -eq "Output") -or ($_ -eq "Host") } { - [int]$intLevel = "2" - break - } - "Significant" { - [int]$intLevel = "3" - break - } - "VeryVerbose" { - [int]$intLevel = "4" - break - } - "Verbose" { - [int]$intLevel = "5" - break - } - "SomewhatVerbose" { - [int]$intLevel = "6" - break - } - "System" { - [int]$intLevel = "7" - break - } - "Debug" { - [int]$intLevel = "8" - break - } - "InternalComment" { - [int]$intLevel = "9" - break - } - default { - [int]$intLevel = "1" - break - } + $logLevels = @{ + "Critical" = "1" + "Debug" = "8" + "Error" = "667" + "Host" = "2" + "Important" = "2" + "InternalComment" = "9" + "Output" = "2" + "Significant" = "3" + "SomewhatVerbose" = "6" + "System" = "7" + "Verbose" = "5" + "VeryVerbose" = "4" + "Warning" = "666" } + [int]$intLevel = $logLevels[$LogLevel] [int]$messageInfo = Get-PSFConfigValue -FullName 'PSFramework.Message.Info.Maximum' if (($messageInfo -lt $intLevel) -or ([PSFramework.Message.MessageHost]::MinimumInformation -gt $intLevel) -and ($intLevel -notin 666..667)) { # Message is below desired log verbosity, skip @@ -125,8 +100,21 @@ # Generate unique logTag information, used to identify each log entry $logTag = (New-Guid).Guid + '-' + (Get-Date).TimeOfDay.TotalMilliseconds # Pass information to Write-PSFMessage, to emit local log - Write-PSFMessage -Level $LogLevel -String $LogString -StringValues $LogStringValues -Tag $logTag -ModuleName $ModuleName -FunctionName $FunctionName -File $File -Line $Line -Target $Target -ErrorRecord $ErrorRecord -Data $Data - if ($env:APPLICATIONINSIGHTS_CONNECTION_STRING -ne '' -and $ApplicationInsights) { + $params = @{ + Level = $LogLevel + String = $LogString + StringValues = $LogStringValues + Tag = $logTag + ModuleName = $ModuleName + FunctionName = $FunctionName + File = $File + Line = $Line + Target = $Target + ErrorRecord = $ErrorRecord + Data = $Data + } + Write-PSFMessage @params + if ($env:APPLICATIONINSIGHTS_CONNECTION_STRING -ne '' -and $ApplicationInsights -eq $true) { # Initiate export of log to ApplicationInsights try { # Gather log generated by Write-PSFMessage with retry/backoff logic