Skip to content

Commit ec5b50a

Browse files
committed
Add Application Insights console logging integration
Introduces Enable-CippConsoleLogging.ps1 to override console output functions and send logs to Application Insights. Updates profile.ps1 to enable this logging when the connection string is present, improving telemetry and diagnostics.
1 parent d74d202 commit ec5b50a

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
function Enable-CippConsoleLogging {
2+
<#
3+
.SYNOPSIS
4+
Enable console output logging to Application Insights
5+
.DESCRIPTION
6+
Overrides Write-Information, Write-Warning, Write-Error, Write-Verbose, and Write-Debug
7+
functions to send telemetry to Application Insights while maintaining normal console output
8+
.FUNCTIONALITY
9+
Internal
10+
.EXAMPLE
11+
Enable-CippConsoleLogging
12+
13+
# Now all Write-* calls will be logged to Application Insights
14+
Write-Information "This will be logged"
15+
Write-Warning "This warning will be logged"
16+
#>
17+
[CmdletBinding()]
18+
param()
19+
20+
# Store the original functions
21+
if (-not $global:CippOriginalWriteFunctions) {
22+
$global:CippOriginalWriteFunctions = @{
23+
Information = Get-Command Write-Information -CommandType Cmdlet
24+
Warning = Get-Command Write-Warning -CommandType Cmdlet
25+
Error = Get-Command Write-Error -CommandType Cmdlet
26+
Verbose = Get-Command Write-Verbose -CommandType Cmdlet
27+
Debug = Get-Command Write-Debug -CommandType Cmdlet
28+
Host = Get-Command Write-Host -CommandType Cmdlet
29+
}
30+
}
31+
32+
# Define log level enum
33+
enum CippConsoleLogLevel {
34+
Debug = 0
35+
Verbose = 1
36+
Information = 2
37+
Warning = 3
38+
Error = 4
39+
}
40+
41+
# Set minimum log level from environment variable (default: Information)
42+
$configuredLevel = $env:CIPP_CONSOLE_LOG_LEVEL
43+
$global:CippConsoleLogMinLevel = if ($configuredLevel) {
44+
try {
45+
[CippConsoleLogLevel]$configuredLevel
46+
} catch {
47+
[CippConsoleLogLevel]::Information
48+
}
49+
} else {
50+
[CippConsoleLogLevel]::Information
51+
}
52+
53+
# Helper function to send log to Application Insights
54+
$global:SendCippConsoleLog = {
55+
param([string]$Message, [CippConsoleLogLevel]$Level)
56+
57+
if ($global:TelemetryClient) {
58+
try {
59+
# Check if this level should be logged
60+
if ($Level -ge $global:CippConsoleLogMinLevel) {
61+
$props = New-Object 'System.Collections.Generic.Dictionary[string,string]'
62+
$props['Message'] = $Message
63+
$props['Level'] = $Level.ToString()
64+
$props['Timestamp'] = (Get-Date).ToString('o')
65+
66+
$global:TelemetryClient.TrackEvent('CIPP.ConsoleLog', $props, $null)
67+
}
68+
} catch {
69+
# Silently fail to avoid infinite loops
70+
}
71+
}
72+
}
73+
74+
# Override Write-Information
75+
function global:Write-Information {
76+
[CmdletBinding()]
77+
param(
78+
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
79+
[object]$MessageData,
80+
[string[]]$Tags
81+
)
82+
83+
# Send to telemetry
84+
& $global:SendCippConsoleLog -Message ($MessageData | Out-String).Trim() -Level ([CippConsoleLogLevel]::Information)
85+
86+
# Call original function
87+
& $global:CippOriginalWriteFunctions.Information @PSBoundParameters
88+
}
89+
90+
# Override Write-Warning
91+
function global:Write-Warning {
92+
[CmdletBinding()]
93+
param(
94+
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
95+
[string]$Message
96+
)
97+
98+
# Send to telemetry
99+
& $global:SendCippConsoleLog -Message $Message -Level ([CippConsoleLogLevel]::Warning)
100+
101+
# Call original function
102+
& $global:CippOriginalWriteFunctions.Warning @PSBoundParameters
103+
}
104+
105+
# Override Write-Error
106+
function global:Write-Error {
107+
[CmdletBinding()]
108+
param(
109+
[Parameter(Position = 0, ValueFromPipeline)]
110+
[object]$Message,
111+
[object]$Exception,
112+
[object]$ErrorRecord,
113+
[string]$ErrorId,
114+
[System.Management.Automation.ErrorCategory]$Category,
115+
[object]$TargetObject,
116+
[string]$RecommendedAction,
117+
[string]$CategoryActivity,
118+
[string]$CategoryReason,
119+
[string]$CategoryTargetName,
120+
[string]$CategoryTargetType
121+
)
122+
123+
# Send to telemetry
124+
$errorMessage = if ($Message) { ($Message | Out-String).Trim() }
125+
elseif ($Exception) { $Exception.Message }
126+
elseif ($ErrorRecord) { $ErrorRecord.Exception.Message }
127+
else { 'Unknown error' }
128+
& $global:SendCippConsoleLog -Message $errorMessage -Level ([CippConsoleLogLevel]::Error)
129+
130+
# Call original function
131+
& $global:CippOriginalWriteFunctions.Error @PSBoundParameters
132+
}
133+
134+
# Override Write-Verbose
135+
function global:Write-Verbose {
136+
[CmdletBinding()]
137+
param(
138+
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
139+
[string]$Message
140+
)
141+
142+
# Send to telemetry
143+
& $global:SendCippConsoleLog -Message $Message -Level ([CippConsoleLogLevel]::Verbose)
144+
145+
# Call original function
146+
& $global:CippOriginalWriteFunctions.Verbose @PSBoundParameters
147+
}
148+
149+
# Override Write-Debug
150+
function global:Write-Debug {
151+
[CmdletBinding()]
152+
param(
153+
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
154+
[string]$Message
155+
)
156+
157+
# Send to telemetry
158+
& $global:SendCippConsoleLog -Message $Message -Level ([CippConsoleLogLevel]::Debug)
159+
160+
# Call original function
161+
& $global:CippOriginalWriteFunctions.Debug @PSBoundParameters
162+
}
163+
164+
# Override Write-Host
165+
function global:Write-Host {
166+
[CmdletBinding()]
167+
param(
168+
[Parameter(Position = 0, ValueFromPipeline)]
169+
[object]$Object,
170+
[switch]$NoNewline,
171+
[object]$Separator,
172+
[System.ConsoleColor]$ForegroundColor,
173+
[System.ConsoleColor]$BackgroundColor
174+
)
175+
176+
# Send to telemetry
177+
$message = if ($Object) { ($Object | Out-String).Trim() } else { '' }
178+
& $global:SendCippConsoleLog -Message $message -Level ([CippConsoleLogLevel]::Information)
179+
180+
# Call original function
181+
& $global:CippOriginalWriteFunctions.Host @PSBoundParameters
182+
}
183+
}

profile.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ if (!$LastStartup -or $CurrentVersion -ne $LastStartup.Version) {
100100
Remove-AzDataTableEntity @ReleaseTable -Entity @{ PartitionKey = 'GitHubReleaseNotes'; RowKey = 'GitHubReleaseNotes' } -ErrorAction SilentlyContinue
101101
Write-Host 'Cleared GitHub release notes cache to force refresh on version update.'
102102
}
103+
104+
# Enable console logging to Application Insights
105+
if ($env:APPLICATIONINSIGHTS_CONNECTION_STRING) {
106+
try {
107+
Enable-CippConsoleLogging
108+
Write-Information 'Console logging to Application Insights enabled'
109+
} catch {
110+
Write-Warning "Failed to enable console logging: $($_.Exception.Message)"
111+
}
112+
}
113+
103114
# Uncomment the next line to enable legacy AzureRm alias in Azure PowerShell.
104115
# Enable-AzureRmAlias
105116

0 commit comments

Comments
 (0)