Skip to content

Commit daf52fb

Browse files
committed
Refactor console logging to use Send-CippConsoleLog
Moved the helper function for sending console logs to Application Insights into a dedicated Send-CippConsoleLog.ps1 file. Updated Enable-CippConsoleLogging.ps1 to use the new function, improving modularity and maintainability.
1 parent d0c0d99 commit daf52fb

File tree

2 files changed

+59
-41
lines changed

2 files changed

+59
-41
lines changed

Modules/CIPPCore/Public/Tools/Enable-CippConsoleLogging.ps1

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,6 @@ function Enable-CippConsoleLogging {
3535
'Information'
3636
}
3737

38-
# Helper function to send log to Application Insights
39-
$global:SendCippConsoleLog = {
40-
param([string]$Message, [string]$Level)
41-
42-
if ($global:TelemetryClient) {
43-
try {
44-
# Map level names to numeric values for comparison
45-
$levelMap = @{
46-
'Debug' = 0
47-
'Verbose' = 1
48-
'Information' = 2
49-
'Warning' = 3
50-
'Error' = 4
51-
}
52-
53-
$currentLevelValue = $levelMap[$Level]
54-
$minLevelValue = $levelMap[$global:CippConsoleLogMinLevel]
55-
56-
# Check if this level should be logged
57-
if ($currentLevelValue -ge $minLevelValue) {
58-
$props = New-Object 'System.Collections.Generic.Dictionary[string,string]'
59-
$props['Message'] = $Message
60-
$props['Level'] = $Level
61-
$props['Timestamp'] = (Get-Date).ToString('o')
62-
63-
$global:TelemetryClient.TrackEvent('CIPP.ConsoleLog', $props, $null)
64-
}
65-
} catch {
66-
# Silently fail to avoid infinite loops
67-
}
68-
}
69-
}
70-
7138
# Override Write-Information
7239
function global:Write-Information {
7340
[CmdletBinding()]
@@ -77,9 +44,9 @@ function Enable-CippConsoleLogging {
7744
[string[]]$Tags
7845
)
7946

80-
# Send to telemetry if MessageData is string or can be converted to string and is not empty, null, or whitespace
47+
# Send to telemetry
8148
if ($MessageData -and -not [string]::IsNullOrWhiteSpace(($MessageData | Out-String).Trim())) {
82-
& $global:SendCippConsoleLog -Message ($MessageData | Out-String).Trim() -Level 'Information'
49+
Send-CippConsoleLog -Message ($MessageData | Out-String).Trim() -Level 'Information'
8350
}
8451

8552
# Call original function
@@ -94,9 +61,9 @@ function Enable-CippConsoleLogging {
9461
[string]$Message
9562
)
9663

97-
# Send to telemetry if Message is not empty, null, or whitespace
64+
# Send to telemetry
9865
if ($Message -and -not [string]::IsNullOrWhiteSpace($Message)) {
99-
& $global:SendCippConsoleLog -Message $Message -Level 'Warning'
66+
Send-CippConsoleLog -Message $Message -Level 'Warning'
10067
}
10168

10269
# Call original function
@@ -128,7 +95,7 @@ function Enable-CippConsoleLogging {
12895
else { 'Unknown error' }
12996

13097
if ($errorMessage -and -not [string]::IsNullOrWhiteSpace($errorMessage)) {
131-
& $global:SendCippConsoleLog -Message $errorMessage -Level 'Error'
98+
Send-CippConsoleLog -Message $errorMessage -Level 'Error'
13299
}
133100

134101
# Call original function
@@ -145,7 +112,7 @@ function Enable-CippConsoleLogging {
145112

146113
# Send to telemetry
147114
if ($Message -and -not [string]::IsNullOrWhiteSpace($Message)) {
148-
& $global:SendCippConsoleLog -Message $Message -Level 'Verbose'
115+
Send-CippConsoleLog -Message $Message -Level 'Verbose'
149116
}
150117

151118
# Call original function
@@ -162,7 +129,7 @@ function Enable-CippConsoleLogging {
162129

163130
# Send to telemetry
164131
if ($Message -and -not [string]::IsNullOrWhiteSpace($Message)) {
165-
& $global:SendCippConsoleLog -Message $Message -Level 'Debug'
132+
Send-CippConsoleLog -Message $Message -Level 'Debug'
166133
}
167134

168135
# Call original function
@@ -184,7 +151,7 @@ function Enable-CippConsoleLogging {
184151
# Send to telemetry
185152
$message = if ($Object) { ($Object | Out-String).Trim() } else { '' }
186153
if ($message -and -not [string]::IsNullOrWhiteSpace($message)) {
187-
& $global:SendCippConsoleLog -Message $message -Level 'Information'
154+
Send-CippConsoleLog -Message $message -Level 'Information'
188155
}
189156

190157
# Call original function
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function Send-CippConsoleLog {
2+
<#
3+
.SYNOPSIS
4+
Send console log message to Application Insights
5+
.DESCRIPTION
6+
Helper function to send console output to Application Insights telemetry
7+
.PARAMETER Message
8+
The message to log
9+
.PARAMETER Level
10+
The log level (Debug, Verbose, Information, Warning, Error)
11+
.FUNCTIONALITY
12+
Internal
13+
#>
14+
[CmdletBinding()]
15+
param(
16+
[Parameter(Mandatory = $true)]
17+
[string]$Message,
18+
19+
[Parameter(Mandatory = $true)]
20+
[ValidateSet('Debug', 'Verbose', 'Information', 'Warning', 'Error')]
21+
[string]$Level
22+
)
23+
24+
if ($global:TelemetryClient) {
25+
try {
26+
# Map level names to numeric values for comparison
27+
$levelMap = @{
28+
'Debug' = 0
29+
'Verbose' = 1
30+
'Information' = 2
31+
'Warning' = 3
32+
'Error' = 4
33+
}
34+
35+
$currentLevelValue = $levelMap[$Level]
36+
$minLevelValue = $levelMap[$global:CippConsoleLogMinLevel]
37+
38+
# Check if this level should be logged
39+
if ($null -ne $minLevelValue -and $currentLevelValue -ge $minLevelValue) {
40+
$props = New-Object 'System.Collections.Generic.Dictionary[string,string]'
41+
$props['Message'] = $Message
42+
$props['Level'] = $Level
43+
$props['Timestamp'] = (Get-Date).ToString('o')
44+
45+
$global:TelemetryClient.TrackEvent('CIPP.ConsoleLog', $props, $null)
46+
}
47+
} catch {
48+
# Silently fail to avoid infinite loops
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)