Scripts/Write-NFSv3ExtendedGroupsLog.ps1

function Write-NFSv3ExtendedGroupsLog {
    param (
        [string]$Message,
        [string]$LogLevel = "INFO",
        [string]$LogFile = $Global:NFSv3ExtendedGroupsLogFile
    )

    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff"
    $logEntry = "[$timestamp] [$PID] [$LogLevel] $Message"

    if ($LogLevel -eq "Error") {
        Write-Error $logEntry
    }
    
    # Take lock on the file so that only one process can update it at time.
    $mutex = New-Object System.Threading.Mutex $false, "Global\MyLogMutex"
    $mutex.WaitOne()

    try {    
        Add-Content -Path $LogFile -Value $logEntry | Out-Null
    } 
    finally {
        # Release the mutex after writing
        $mutex.ReleaseMutex() | Out-Null
    }
}