diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fa4067..874540f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Fixes +- Capture doesn't work if `-Debug` wasn't set ([#74](https://github.com/getsentry/sentry-powershell/pull/74)) - Don't log debug messages if `-Debug` wasn't set ([#75](https://github.com/getsentry/sentry-powershell/pull/75)) ### Dependencies diff --git a/modules/Sentry/private/DiagnosticLogger.ps1 b/modules/Sentry/private/DiagnosticLogger.ps1 index ab4efc7..1dd2485 100644 --- a/modules/Sentry/private/DiagnosticLogger.ps1 +++ b/modules/Sentry/private/DiagnosticLogger.ps1 @@ -55,7 +55,7 @@ class DiagnosticLogger : Sentry.Extensibility.IDiagnosticLogger # see https://github.com/PowerShell/PowerShell/issues/5148 if ($global:PSVersionTable.PSEdition -eq 'Desktop') { $pref = Get-Variable DebugPreference - if ($pref.value -eq "Inquire") { + if (($null -ne $pref) -and ($pref.value -eq "Inquire")) { $DebugPreference = 'Continue' } } diff --git a/modules/Sentry/private/StackTraceProcessor.ps1 b/modules/Sentry/private/StackTraceProcessor.ps1 index 4b49e8b..e6ac49d 100644 --- a/modules/Sentry/private/StackTraceProcessor.ps1 +++ b/modules/Sentry/private/StackTraceProcessor.ps1 @@ -4,11 +4,17 @@ class StackTraceProcessor : SentryEventProcessor [System.Management.Automation.InvocationInfo]$InvocationInfo [System.Management.Automation.CallStackFrame[]]$StackTraceFrames [string[]]$StackTraceString + hidden [Sentry.Extensibility.IDiagnosticLogger] $logger hidden [string[]] $modulePaths hidden [hashtable] $pwshModules = @{} - StackTraceProcessor() + StackTraceProcessor([Sentry.SentryOptions] $options) { + $this.logger = $options.DiagnosticLogger + if ($null -eq $this.logger) { + $this.logger = Get-Variable -Scope script -Name SentryPowerShellDiagnosticLogger -ValueOnly -ErrorAction SilentlyContinue + } + if ($env:PSModulePath.Contains(';')) { # Windows @@ -369,7 +375,13 @@ class StackTraceProcessor : SentryEventProcessor { if ($lines.Count -lt $sentryFrame.LineNumber) { - Write-Debug "Couldn't set frame context because the line number ($($sentryFrame.LineNumber)) is lower than the available number of source code lines ($($lines.Count))." + if ($null -ne $this.logger) { + $this.logger.Log( + [Sentry.SentryLevel]::Debug, + "Couldn't set frame context because the line number ($($sentryFrame.LineNumber)) " + + "is lower than the available number of source code lines ($($lines.Count))." + ) + } return } diff --git a/modules/Sentry/private/SynchronousTransport.ps1 b/modules/Sentry/private/SynchronousTransport.ps1 index ff8a0b2..0f3528b 100644 --- a/modules/Sentry/private/SynchronousTransport.ps1 +++ b/modules/Sentry/private/SynchronousTransport.ps1 @@ -11,6 +11,9 @@ class SynchronousTransport : Sentry.Http.HttpTransportBase, Sentry.Extensibility SynchronousTransport([Sentry.SentryOptions] $options) : base($options) { $this.logger = $options.DiagnosticLogger + if ($null -eq $this.logger) { + $this.logger = Get-Variable -Scope script -Name SentryPowerShellDiagnosticLogger -ValueOnly -ErrorAction SilentlyContinue + } # These are internal methods, so we need to use reflection to access them. $instanceMethod = [System.Reflection.BindingFlags]::Instance + [System.Reflection.BindingFlags]::NonPublic + [System.Reflection.BindingFlags]::Public; @@ -43,7 +46,9 @@ class SynchronousTransport : Sentry.Http.HttpTransportBase, Sentry.Extensibility $content = $reader.ReadToEnd() $reader.Close() - $this.logger.Log([Sentry.SentryLevel]::Debug, 'Sending content synchronously, Content-Length: {0}', $null, $content.Length) + if ($null -ne $this.logger) { + $this.logger.Log([Sentry.SentryLevel]::Debug, 'Sending content synchronously, Content-Length: {0}', $null, $content.Length) + } $ProgressPreference = 'SilentlyContinue' $psResponse = Invoke-WebRequest -Uri $request.RequestUri -Method $request.Method.Method -Headers $headers -Body $content -UseBasicParsing diff --git a/modules/Sentry/public/Out-Sentry.ps1 b/modules/Sentry/public/Out-Sentry.ps1 index 389bcf4..1b87a54 100644 --- a/modules/Sentry/public/Out-Sentry.ps1 +++ b/modules/Sentry/public/Out-Sentry.ps1 @@ -29,12 +29,18 @@ function Out-Sentry { if (-not [Sentry.SentrySdk]::IsEnabled) { + # Workaround for: + # NullReferenceException: Object reference not set to an instance of an object. + # at Out-Sentry, D:\a\sentry-powershell\sentry-powershell\modules\Sentry\public\Out-Sentry.ps1:32 + try { + Write-Debug "Sentry is not started: Out-Sentry invocation ignored." + } catch {} return } $options = Get-CurrentOptions [Sentry.SentryEvent]$event_ = $null - $processor = [StackTraceProcessor]::new() + $processor = [StackTraceProcessor]::new($options) if ($ErrorRecord -ne $null) { diff --git a/modules/Sentry/public/Start-Sentry.ps1 b/modules/Sentry/public/Start-Sentry.ps1 index 91d7c68..3545699 100644 --- a/modules/Sentry/public/Start-Sentry.ps1 +++ b/modules/Sentry/public/Start-Sentry.ps1 @@ -47,7 +47,11 @@ function Start-Sentry } $logger = [DiagnosticLogger]::new($options.DiagnosticLevel) + + # Note: this is currently a no-op if options.debug == false; see https://github.com/getsentry/sentry-dotnet/issues/3212 + # As a workaround, we set the logger as a global variable so that we can reach it in other scripts. $options.DiagnosticLogger = $logger + $script:SentryPowerShellDiagnosticLogger = $logger if ($null -eq $options.Transport) { diff --git a/modules/Sentry/public/Stop-Sentry.ps1 b/modules/Sentry/public/Stop-Sentry.ps1 index d748ecf..32ba954 100644 --- a/modules/Sentry/public/Stop-Sentry.ps1 +++ b/modules/Sentry/public/Stop-Sentry.ps1 @@ -1,4 +1,5 @@ function Stop-Sentry { [Sentry.SentrySdk]::Close() + Remove-Variable -Scope script -Name SentryPowerShellDiagnosticLogger -ErrorAction SilentlyContinue } diff --git a/tests/stacktrace-processor.tests.ps1 b/tests/stacktrace-processor.tests.ps1 index a48c055..cd5faf2 100644 --- a/tests/stacktrace-processor.tests.ps1 +++ b/tests/stacktrace-processor.tests.ps1 @@ -8,7 +8,7 @@ Describe 'StackTraceProcessor' { $event_.Message = 'Test' $event_.Level = [Sentry.SentryLevel]::Info - $sut = [StackTraceProcessor]::new() + $sut = [StackTraceProcessor]::new([Sentry.SentryOptions]::new()) $sut.StackTraceString = 'at funcB, C:\dev\sentry-powershell\tests\throwing.ps1: line 17 at , : line 1 at , : line 3' -split "[`r`n]+"