Framework/Abstracts/FileOutputBase.ps1

Set-StrictMode -Version Latest 
class FileOutputBase: ListenerBase
{
    static [string] $ETCFolderPath = "Etc";

    [string] $FilePath = "";
    [string] $FolderPath = "";
    [string] $BasePath = "";
    
    FileOutputBase()
    {   
        [Helpers]::AbstractClass($this, [FileOutputBase]);
    }     

    [string] CalculateFolderPath([SubscriptionContext] $context, [string] $subFolderPath)
    {
        $outputPath = "";
        if($context -and (-not [string]::IsNullOrWhiteSpace($context.SubscriptionName)) -and (-not [string]::IsNullOrWhiteSpace($context.SubscriptionId)))
        {
            $isDefaultPath = $false;
            if([System.String]::IsNullOrEmpty($this.BasePath))
            {
                $isDefaultPath = $true;
                $this.BasePath = [Constants]::AzSdkLogFolderPath;
            }

            if (-not $this.BasePath.EndsWith("\")) {
                $this.BasePath += "\";
            }

            $outputPath = $this.BasePath + "AzSDKLogs\"

            $sanitizedPath = [Helpers]::SanitizeFolderName($context.SubscriptionName);
            if ([string]::IsNullOrEmpty($sanitizedPath)) {
                $sanitizedPath = $context.SubscriptionId;
            }

            if ([string]::IsNullOrEmpty($sanitizedPath)) {
                $outputPath += ("Default\{0}\" -f $this.RunIdentifier);            
            }
            else {
                $outputPath += ("Sub_{0}\{1}\" -f $sanitizedPath, $this.RunIdentifier);            
            }

            if (-not [string]::IsNullOrEmpty($subFolderPath)) {
                $sanitizedPath = [Helpers]::SanitizeFolderName($subFolderPath);
                if (-not [string]::IsNullOrEmpty($sanitizedPath)) {
                    $outputPath += ("{0}\" -f $sanitizedPath);            
                }   
            }

            if(-not (Test-Path $outputPath))
            {
                try
                {
                    mkdir -Path $outputPath -ErrorAction Stop | Out-Null
                }
                catch
                {
                    $outputPath = "";
                    if(-not $isDefaultPath)
                    {
                        $this.BasePath = "";
                        $outputPath = $this.CalculateFolderPath($context, $subFolderPath);
                    }
                }
            }
        }
        return $outputPath;
    }

    [string] CalculateFolderPath([SubscriptionContext] $context)
    {
        return $this.CalculateFolderPath($context, "");
    }

    [void] SetFolderPath([SubscriptionContext] $context)
    {
        $this.SetFolderPath($context, "");
    }

    [void] SetFolderPath([SubscriptionContext] $context, [string] $subFolderPath)
    {
        $this.FolderPath = $this.CalculateFolderPath($context, $subFolderPath);
    }

    [string] CalculateFilePath([SubscriptionContext] $context, [string] $fileName)
    {
        return $this.CalculateFilePath($context, "", $fileName);
    }

    [string] CalculateFilePath([SubscriptionContext] $context, [string] $subFolderPath, [string] $fileName)
    {
        $outputPath = "";
        $this.SetFolderPath($context, $subFolderPath); 
        if ([string]::IsNullOrEmpty($this.FolderPath)) {
            return $outputPath;
        }

        $outputPath = $this.FolderPath;
        if (-not $outputPath.EndsWith("\")) {
            $outputPath += "\";
        }
        if ([string]::IsNullOrEmpty($fileName)) {
            $outputPath += $(Get-Date -format "yyyyMMdd_HHmmss") + ".log";
        }
        else {
            $outputPath += $fileName;            
        }
        return $outputPath;
    }

    [void] SetFilePath([SubscriptionContext] $context, [string] $fileName)
    {
        $this.SetFilePath($context, "", $fileName);
    }

    [void] SetFilePath([SubscriptionContext] $context, [string] $subFolderPath, [string] $fileName)
    {
        $this.FilePath = $this.CalculateFilePath($context, $subFolderPath, $fileName);
    }
}