Powershell logging to Azure Application Insight
A Powershell Core Module that simplifies logging to Application Insights. This uses the Built-in Telemetry client from Powershell Core 7 and it is built entirely in Powershell. It offers a few simple functions to log information to Application Insights to reduce the clutter in your scripts.
Install-Module -Name PSCoreApplicationInsights
Create a new Application Insights Client
New-ApplicationInsightsClient [-InstrumentationKey] <Guid> [-WhatIf] [-Confirm] [<CommonParameters>]
Use the Instrumentation Key found in the Azure Portal on your Application Insights Instance.
The Application Insights client is stored as $global:AIClient.
to store the client in a variable to specify when writing logs:
$client = New-ApplicationInsightsClient -InstrumentationKey c323cf10-da34-4a73-9eac-000000000000
Write-ApplicationInsightsTrace [[-Client] <TelemetryClient>] [-Message] <String> [[-SeverityLevel] <String>] [[-properties] <System.Collections.Generic.Dictionary`2[System.String,System.String]>] [<CommonParameters>]
Property | Description | Mandatory | default | Allowed Values |
---|---|---|---|---|
Message | true | |||
Client | The Application Insights client to write the message to. If not specifies, uses the $global:AIclient | false | ||
SeverityLevel | The severity level of the trace | false | Information | - Information - Verbose - Warning - Error - Critical |
properties | a Dictionary<string,string> with custom properties that will be added as "customDimensions" | false |
Write-ApplicationInsightsTrace -Message "This is a test message as Critical" -SeverityLevel "Critical"
Result:
$properties = [System.Collections.Generic.Dictionary[string, string]]::new()
$properties.Add("target", "azkv-powershell-001")
$properties.Add("type", "Keyvault")
Write-ApplicationInsightsTrace -Client $client -Message "Created new keyvault" -SeverityLevel "Information" -properties $properties
Result:
Create a new Application insights Client by supplying an Instrumentation Key of your Application Insights instance.
New-ApplicationInsightsClient [-InstrumentationKey] <Guid> [-WhatIf] [-Confirm] [<CommonParameters>]
New-ApplicationInsightsClient -InstrumentationKey c323cf10-da34-4a73-9eac-000000000000
$client = New-ApplicationInsightsClient -InstrumentationKey c323cf10-da34-4a73-9eac-000000000000
The Instrumentation Key of your Application Insights instance.
Type: | Guid |
PipelineInput : | false |
Position : | 1 |
Required : | true |
Invoke a scriptblock that is measured by Application Insights. This created a timespan and writes the timing to Application Insights. The output of the scriptblock is returned.
Invoke-ApplicationInsightsMeasuredCommand [[-Client] <TelemetryClient>] [-scriptblock] <ScriptBlock> [-name] <String> [<CommonParameters>]
Invoke-ApplicationInsightsMeasuredCommand -ScriptBlock { start-sleep -seconds 1 } -Name "slow script"
The Application Insights Telemetry Client. Defaults to $global:AIClient
Type: | TelemetryClient |
PipelineInput : | false |
Position : | 1 |
Required : | false |
Example Logs:
Example of the Performance blade
Performance is shown on the Overview Blade
- Automate Deployment
- Write Documentation for each function
- Creating a new Application Insights Client
- Setting Client information
- Sending Trace
- Sending Metric
- Sending Exception
- Sending Request
- Invoking Measured Command
- Create an example azure dashboard