Framework/Listeners/UserReports/WriteDetailedLog.ps1
Set-StrictMode -Version Latest class WriteDetailedLog: FileOutputBase { hidden static [WriteDetailedLog] $Instance = $null; static [WriteDetailedLog] GetInstance() { if ( $null -eq [WriteDetailedLog]::Instance) { [WriteDetailedLog]::Instance = [WriteDetailedLog]::new(); } return [WriteDetailedLog]::Instance } [void] RegisterEvents() { $this.UnregisterEvents(); $this.RegisterEvent([AzSdkRootEvent]::GenerateRunIdentifier, { $currentInstance = [WriteDetailedLog]::GetInstance(); try { $currentInstance.SetRunIdentifier([AzSdkRootEventArgument] ($Event.SourceArgs | Select-Object -First 1)); } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::EvaluationStarted, { $currentInstance = [WriteDetailedLog]::GetInstance(); try { if(-not [string]::IsNullOrWhiteSpace($Event.SourceArgs.ResourceContext.ResourceGroupName) -and -not [string]::IsNullOrWhiteSpace($Event.SourceArgs.ResourceContext.ResourceName)) { $currentInstance.SetFilePath($Event.SourceArgs.ResourceContext, $Event.SourceArgs.ResourceContext.ResourceGroupName, ($Event.SourceArgs.ResourceContext.FeatureName + ".log")); $startHeading = ([Constants]::ModuleStartHeading -f $Event.SourceArgs.ResourceContext.FeatureName, $Event.SourceArgs.ResourceContext.ResourceGroupName, $Event.SourceArgs.ResourceContext.ResourceName); } else { $currentInstance.SetFilePath($Event.SourceArgs.ResourceContext, $Event.SourceArgs.ResourceContext.SubscriptionName, ($Event.SourceArgs.ResourceContext.FeatureName + ".log")); $startHeading = ([Constants]::ModuleStartHeadingSub -f $Event.SourceArgs.ResourceContext.FeatureName, $Event.SourceArgs.ResourceContext.SubscriptionName, $Event.SourceArgs.ResourceContext.SubscriptionId); } $currentInstance.AddOutputLog($startHeading); } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::EvaluationCompleted, { $currentInstance = [WriteDetailedLog]::GetInstance(); try { $props = $Event.SourceArgs[0]; if(-not [string]::IsNullOrWhiteSpace($Event.SourceArgs.ResourceContext.ResourceGroupName) -and -not [string]::IsNullOrWhiteSpace($Event.SourceArgs.ResourceContext.ResourceName)) { $currentInstance.AddOutputLog(([Constants]::CompletedAnalysis -f $props.ResourceContext.FeatureName, $props.ResourceContext.ResourceGroupName, $props.ResourceContext.ResourceName)); } else { $currentInstance.AddOutputLog(([Constants]::CompletedAnalysisSub -f $props.ResourceContext.FeatureName, $props.ResourceContext.SubscriptionName, $props.ResourceContext.SubscriptionId)); } $currentInstance.AddOutputLog([Constants]::HashLine); $currentInstance.FilePath = ""; } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::ControlStarted, { $currentInstance = [WriteDetailedLog]::GetInstance(); try { $currentInstance.AddOutputLog([Constants]::DoubleDashLine); $currentInstance.AddOutputLog("[$($Event.SourceArgs.ControlItem.ControlID)]: $($Event.SourceArgs.ControlItem.Description)"); $currentInstance.AddOutputLog([Constants]::SingleDashLine); if(-not [string]::IsNullOrWhiteSpace($Event.SourceArgs.ResourceContext.ResourceName)) { $currentInstance.AddOutputLog(("Info: Checking: [{0}]-[$($Event.SourceArgs.ControlItem.Description)] for resource [{1}]" -f $Event.SourceArgs.ResourceContext.FeatureName, $Event.SourceArgs.ResourceContext.ResourceName), $true); } else { $currentInstance.AddOutputLog(("Info: Checking: [{0}]-[$($Event.SourceArgs.ControlItem.Description)] for subscription [{1}]" -f $Event.SourceArgs.ResourceContext.FeatureName, $Event.SourceArgs.ResourceContext.SubscriptionName), $true); } } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([SVTEvent]::ControlCompleted, { $currentInstance = [WriteDetailedLog]::GetInstance(); try { $currentInstance.WriteControlResult([SVTEventContext] ($Event.SourceArgs | Select-Object -First 1 )); $currentInstance.AddOutputLog(([Constants]::DoubleDashLine + " `r`n`r`n ")); } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([AzSdkRootEvent]::CommandStarted, { $currentInstance = [WriteDetailedLog]::GetInstance(); try { $currentInstance.SetFilePath($Event.SourceArgs.Context, $Event.SourceArgs.Context.SubscriptionName, "Detailed.log"); } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([AzSdkRootEvent]::CommandProcessing, { $currentInstance = [WriteDetailedLog]::GetInstance(); try { if($Event.SourceArgs.Messages) { $Event.SourceArgs.Messages | ForEach-Object { $currentInstance.AddOutputLog($_); } } } catch { $currentInstance.PublishException($_); } }); $this.RegisterEvent([AzSdkRootEvent]::CommandCompleted, { $currentInstance = [WriteDetailedLog]::GetInstance(); try { if($Event.SourceArgs.Messages) { $Event.SourceArgs.Messages | ForEach-Object { $currentInstance.AddOutputLog($_); } } $currentInstance.FilePath = ""; } catch { $currentInstance.PublishException($_); } }); } hidden [void] AddOutputLog([string] $message, [bool] $includeTimeStamp) { if([string]::IsNullOrEmpty($message) -or [string]::IsNullOrEmpty($this.FilePath)) { return; } if($includeTimeStamp) { $message = (Get-Date -format "MM\/dd\/yyyy HH:mm:ss") + "-" + $message } Add-Content -Value $message -Path $this.FilePath } hidden [void] AddOutputLog([string] $message) { $this.AddOutputLog($message, $false); } hidden [void] AddOutputLog([MessageData] $messageData) { if($messageData) { if (-not [string]::IsNullOrEmpty($messageData.Message)) { $this.AddOutputLog("`r`n`r`n" + $messageData.Message); } if ($messageData.DataObject) { if (-not [string]::IsNullOrEmpty($messageData.Message)) { $this.AddOutputLog("`r`n"); } $this.AddOutputLog([Helpers]::ConvertObjectToString($messageData.DataObject, $false)); } } } hidden [void] WriteControlResult([SVTEventContext] $eventContext) { if($eventContext.ControlResults -and $eventContext.ControlResults.Count -ne 0) { $controlDesc = $eventContext.ControlItem.Description; $eventContext.ControlResults | Foreach-Object { if(-not [string]::IsNullOrWhiteSpace($_.ChildResourceName) -and $_.ChildResourceName -ne $eventContext.ResourceContext.ResourceName) { $this.AddOutputLog("`r`n"+([Constants]::SingleDashLine)); $this.AddOutputLog(("Info: Analyzing control: [{0}]-[$controlDesc] for resource [{1}]" -f $eventContext.ResourceContext.FeatureName, $_.ChildResourceName), $true); } $_.Messages | ForEach-Object { $this.AddOutputLog($_); } $this.AddOutputLog("`r`n"); $this.AddOutputLog([Constants]::SingleDashLine); if(-not [string]::IsNullOrWhiteSpace($_.ChildResourceName)) { $this.AddOutputLog(("**{3}**: [{0}]-[{2}] for resource: [{1}]" -f $eventContext.ResourceContext.FeatureName, $_.ChildResourceName, $eventContext.ControlItem.Description, $_.VerificationResult.ToString())); } else { $this.AddOutputLog(("**{3}**: [{0}]-[{2}] for subscription: [{1}]" -f $eventContext.ResourceContext.FeatureName, $eventContext.ResourceContext.SubscriptionName, $eventContext.ControlItem.Description, $_.VerificationResult.ToString())); } } } else { $this.AddOutputLog("`r`n"); $this.AddOutputLog([Constants]::SingleDashLine); $this.AddOutputLog(("**Disabled**: [{0}]-[{1}]" -f $eventContext.ResourceContext.FeatureName, $eventContext.ControlItem.Description)); } $this.AddOutputLog([Constants]::SingleDashLine); } } |