src/Tracing/Trace-XrmFunction.ps1

<#
    .SYNOPSIS
    Output verbose information about function call.

    .DESCRIPTION
    Core module method to trace information and measure calls performance.
    Nothing is written unless verbose output is on (-Verbose or $VerbosePreference): the messages then go to the verbose stream and to the log file (Write-HostAndLog).

    .PARAMETER Name
    Called function name.

    .PARAMETER Stage
    Indicate when tracing is called from function : Start or Stop.

    .PARAMETER Parameters
    Parameters of the called function ($MyInvocation.MyCommand.Parameters). The value of each one is read by name from the calling scope (visible from the module's own functions only) and logged, credentials masked.

    .PARAMETER StopWatch
    Stopwatch object initialized during first function call in order to measure overall duration.

    .OUTPUTS
    System.Void. The trace goes to the verbose stream and to the log file.

    .EXAMPLE
    $stopWatch = [System.Diagnostics.Stopwatch]::StartNew();
    Trace-XrmFunction -Name "Import-ContosoAccounts" -Stage Start -Verbose;
    # Processing
    $stopWatch.Stop();
    Trace-XrmFunction -Name "Import-ContosoAccounts" -Stage Stop -StopWatch $stopWatch -Verbose;

    .LINK
    https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/Trace-XrmFunction.md
#>

function Trace-XrmFunction {
    [CmdletBinding()]
    [OutputType([System.Void])]
    param
    (        
        [Parameter(Mandatory = $true)]
        [String]
        $Name,

        [Parameter(Mandatory = $true)]
        [ValidateSet("Start", "Stop")]
        [String]
        $Stage,

        [Parameter()]
        [object[]]
        $Parameters,

        [Parameter()]
        [System.Diagnostics.Stopwatch]
        $StopWatch
    )
    process {

        # Performance tuning
        $verbose = $VerbosePreference -ne 'SilentlyContinue';
        if (-not $verbose) {
            return;
        }

        $message = "[$Name]`t";
        if ($Stage -eq "Start") {
            $message += "Function started";
        }
        elseif ($Stage -eq "Stop") {
            $message += "Function stopped";  
            
            if ($StopWatch) {
                $message += "`t(Elapsed=$($StopWatch.Elapsed.TotalMilliseconds) ms)";
            }
        }
        else {
            $message += "Function $Stage";   
        }
        Write-HostAndLog -Message $message -Level "VERB" -ForegroundColor Gray;

        if ($Parameters) {
            ($Parameters).Keys | ForEach-Object {
                $value = (Get-Variable -Name $_ -ErrorAction SilentlyContinue).Value;
                if ($value.length -gt 0) {
                    # Credentials stay out of the verbose log and its file
                    if ($_ -match 'Password|Secret|Token|ConnectionString') {
                        $value = "***";
                    }
                    else {
                        # Also inside a value: a connection string, or an object that holds one (Out-XrmConnectionString -XrmInstance, M4)
                        $value = "$value" -replace '(?i)\b(password|pwd|clientsecret|secret|accesstoken|token)(\s*=\s*)("[^"]*"|''[^'']*''|[^;}]*)', '$1$2***';
                    }
                    Write-HostAndLog -Message  "[$Name]`t>`tParam : $($_) => $($value)" -Level "VERB" -ForegroundColor DarkGray;
                }
            }
        }
    }
}

Export-ModuleMember -Function Trace-XrmFunction -Alias *;