Adds exeception detail reporting

This commit is contained in:
Lord Hepipud 2022-01-12 09:53:28 +01:00
parent 45b832ac3d
commit 008b4920f1
4 changed files with 70 additions and 7 deletions

View file

@ -27,6 +27,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
* [#388](https://github.com/Icinga/icinga-powershell-framework/issues/388) Improves performance for testing if `Add-Type` functions have been added, by adding an internal test for newly introduced environment variables within a PowerShell session
* [#417](https://github.com/Icinga/icinga-powershell-framework/issues/417) Adds support to allow the force creation of Icinga Agent certificates, even when they are already present on the system over Icinga Management Console installation
* [#426](https://github.com/Icinga/icinga-powershell-framework/pull/426) Adds a new feature to EventLog writer, allowing to parse an exception report to identity the exact root cause and location, on where the error occurred in first place.
## 1.7.1 (2021-11-11)

View file

@ -0,0 +1,60 @@
function Get-IcingaExceptionString()
{
param (
$ExceptionObject = $null
);
if ($null -eq $ExceptionObject) {
return '';
}
$ExceptionStack = New-Object -TypeName 'System.Text.StringBuilder';
$ExceptionStack.AppendLine('') | Out-Null;
$ExceptionStack.AppendLine('') | Out-Null;
$ExceptionStack.AppendLine('Icinga for Windows exception report:') | Out-Null;
if ([string]::IsNullOrEmpty($ExceptionObject.Exception.Message) -eq $FALSE) {
$ExceptionStack.AppendLine('') | Out-Null;
$ExceptionStack.AppendLine('Exception Message:') | Out-Null;
$ExceptionStack.AppendLine($ExceptionObject.Exception.Message) | Out-Null;
}
if ([string]::IsNullOrEmpty($ExceptionObject.InvocationInfo.InvocationName) -eq $FALSE) {
$ExceptionStack.AppendLine('') | Out-Null;
$ExceptionStack.AppendLine('Invocation Name:') | Out-Null;
$ExceptionStack.AppendLine($ExceptionObject.InvocationInfo.InvocationName) | Out-Null;
}
if ([string]::IsNullOrEmpty($ExceptionObject.InvocationInfo.CommandOrigin) -eq $FALSE) {
$ExceptionStack.AppendLine('') | Out-Null;
$ExceptionStack.AppendLine('Command Origin:') | Out-Null;
$ExceptionStack.AppendLine($ExceptionObject.InvocationInfo.CommandOrigin) | Out-Null;
}
if ([string]::IsNullOrEmpty($ExceptionObject.InvocationInfo.ScriptLineNumber) -eq $FALSE) {
$ExceptionStack.AppendLine('') | Out-Null;
$ExceptionStack.AppendLine('Script Line Number:') | Out-Null;
$ExceptionStack.AppendLine($ExceptionObject.InvocationInfo.ScriptLineNumber) | Out-Null;
}
if ([string]::IsNullOrEmpty($ExceptionObject.InvocationInfo.PositionMessage) -eq $FALSE) {
$ExceptionStack.AppendLine('') | Out-Null;
$ExceptionStack.AppendLine('Exact Position:') | Out-Null;
$ExceptionStack.AppendLine($ExceptionObject.InvocationInfo.PositionMessage) | Out-Null;
}
if ([string]::IsNullOrEmpty($ExceptionObject.Exception.StackTrace) -eq $FALSE) {
$ExceptionStack.AppendLine('') | Out-Null;
$ExceptionStack.AppendLine('StackTrace:') | Out-Null;
$ExceptionStack.AppendLine($ExceptionObject.Exception.StackTrace) | Out-Null;
}
$CallStack = Get-PSCallStack;
$ExceptionStack.AppendLine('') | Out-Null;
$ExceptionStack.AppendLine('Call Stack:') | Out-Null;
if ($CallStack.Count -gt 11) {
$ExceptionStack.AppendLine(($CallStack[0..10] | Out-String)) | Out-Null;
} else {
$ExceptionStack.AppendLine(($CallStack | Out-String)) | Out-Null;
}
$ExceptionStack.Remove($ExceptionStack.Length - 8, 8) | Out-Null;
return $ExceptionStack.ToString();
}

View file

@ -2,7 +2,8 @@ function Write-IcingaDebugMessage()
{
param (
[string]$Message,
[array]$Objects = @()
[array]$Objects = @(),
$ExceptionObject = $null
);
if ([string]::IsNullOrEmpty($Message)) {
@ -16,5 +17,5 @@ function Write-IcingaDebugMessage()
[array]$DebugContent = @($Message);
$DebugContent += $Objects;
Write-IcingaEventMessage -EventId 1000 -Namespace 'Framework' -Objects $DebugContent;
Write-IcingaEventMessage -EventId 1000 -Namespace 'Framework' -ExceptionObject $ExceptionObject -Objects $DebugContent;
}

View file

@ -3,7 +3,8 @@ function Write-IcingaEventMessage()
param (
[int]$EventId = 0,
[string]$Namespace = $null,
[array]$Objects = @()
[array]$Objects = @(),
$ExceptionObject = $null
);
if ($EventId -eq 0 -Or [string]::IsNullOrEmpty($Namespace)) {
@ -25,7 +26,7 @@ function Write-IcingaEventMessage()
if ($Objects.Count -eq 0) {
$ObjectDump = [string]::Format(
'{0}{0}No additional object details provided.',
'{0}No additional object details provided.',
(New-IcingaNewLine)
);
}
@ -39,12 +40,12 @@ function Write-IcingaEventMessage()
}
[string]$EventLogMessage = [string]::Format(
'{0}{1}{1}{2}{1}{1}Object dumps if available:{1}{3}',
'{0}{1}{1}{2}{3}{1}{1}Object details:{1}{4}',
$Message,
(New-IcingaNewLine),
$Details,
(Get-IcingaExceptionString -ExceptionObject $ExceptionObject),
$ObjectDump
);
if ($null -eq $EntryType -Or $null -eq $Message) {