Skip to content

Commit 612ae8a

Browse files
committed
Refactor Application Insights initialization and logging
Updated Get-ApplicationInsightsQuery.ps1 to check for $global:TelemetryClient instead of the connection string environment variable. Moved Enable-CippConsoleLogging calls into the TelemetryClient initialization blocks in profile.ps1 and removed redundant console logging enablement code.
1 parent b8f0d7d commit 612ae8a

File tree

2 files changed

+4
-12
lines changed

2 files changed

+4
-12
lines changed

Modules/CIPPCore/Public/Get-ApplicationInsightsQuery.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ function Get-ApplicationInsightsQuery {
55
[string]$Query
66
)
77

8-
if (!$env:APPLICATIONINSIGHTS_CONNECTION_STRING) {
9-
throw 'Application Insights not enabled for this environment.'
8+
if (!$global:TelemetryClient) {
9+
throw 'Application Insights is not enabled for this instance.'
1010
}
1111

1212
if ($env:MSI_SECRET) {

profile.ps1

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ if (-not $global:TelemetryClient) {
2323
$config = [Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration]::CreateDefault()
2424
$config.ConnectionString = $connectionString
2525
$global:TelemetryClient = [Microsoft.ApplicationInsights.TelemetryClient]::new($config)
26+
Enable-CippConsoleLogging
2627
Write-Information 'TelemetryClient initialized with connection string'
2728
} elseif ($env:APPINSIGHTS_INSTRUMENTATIONKEY) {
2829
# Fall back to instrumentation key
2930
$global:TelemetryClient = [Microsoft.ApplicationInsights.TelemetryClient]::new()
3031
$global:TelemetryClient.InstrumentationKey = $env:APPINSIGHTS_INSTRUMENTATIONKEY
32+
Enable-CippConsoleLogging
3133
Write-Information 'TelemetryClient initialized with instrumentation key'
3234
} else {
3335
Write-Warning 'No Application Insights connection string or instrumentation key found'
@@ -101,16 +103,6 @@ if (!$LastStartup -or $CurrentVersion -ne $LastStartup.Version) {
101103
Write-Host 'Cleared GitHub release notes cache to force refresh on version update.'
102104
}
103105

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-
114106
# Uncomment the next line to enable legacy AzureRm alias in Azure PowerShell.
115107
# Enable-AzureRmAlias
116108

0 commit comments

Comments
 (0)