Skip to content

Only use Write-Debug in catch clauses #986

@DennisL68

Description

@DennisL68

I wrote a command that catches the use of Write-Debug and dumps the output using PSFramework to quickly being able to debug all my legacy code.

function Write-MyDebug {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory, Position = 0)]
        [String]$Message
    )
    $Caller = (Get-PSCallStack)[1].Command

    Write-PSFMessage -Level Debug -Message $Message -Function $Caller

<#

.ForwardHelpTargetName Microsoft.PowerShell.Utility\Write-Debug
.ForwardHelpCategory Cmdlet

#>

}

Set-Alias Write-Debug -Value Write-MyDebug -Scope Global

But the logs now gets dirty as soon as I enter a command in the PowerShell console on systems that has posh-git installed.

Command         Arguments                                                               Location
-------         ---------                                                               --------
Write-MyDebug   {Message=Setting WindowTitle: C:\Users\public - PowerShell 5.1 (22116)} PSFRedirect.psm1: line 138
Set-WindowTitle {GitStatus=, IsAdmin=}                                                  WindowTitle.ps1: line 58
prompt          {}                                                                      posh-git.psm1: line 101
<ScriptBlock>   {}                                                                      <No file>

Apparently, the script WindowTitle.ps1 in posh-git calls Write-Debug at line 58 in the released code.

Write-Debug "Setting WindowTitle: $windowTitleText"

This is where Write-Debug is used throughout the project.

  • Line 78; AnsiUtils.ps1
  • Line 95: GitTabExpansion.ps1
  • Line 18: WindowTitle.ps1
  • Line 19: WindowTitle.ps1
  • Line 24: WindowTitle.ps1
  • Line 37: WindowTitle.ps1
  • Line 58: WindowTitle.ps1
  • Line 62: WindowTitle.ps1

I could of course filter my log output/input each time I'm using this for logging the usage of Write-Debug on my systems. And I know I asked for issues like this by writing such a function.

But it would help me a lot if Write-Debug was only used in catch-clauses when there really is an error to handle.
(This will also enhance the performance some what 👍)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions