Setting Trace Logging for MS Dynamics CRM

# crmTraceLogSettings.ps1

$traceDirectory="D:\crmTraceLogs"
$enable=$false
$categories='*:\Verbose'
$fileSize=10


function setCrmTrace{
param ($enable=$false,
$traceDirectory,
$categories,
$fileSize,
$resetIis=$false,
$verbose=$true)

    $ErrorActionPreference='stop'
    # Source: https://support.microsoft.com/en-us/help/907490/how-to-enable-tracing-in-microsoft-dynamics-crm
    $serverTracingRegistry='Registry::HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\MSCRM'
    # Ignore these for newer CRM versions
    #$clientForOutlookTracingRegistry='Registry::HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\MSCRMClient'
    #$ssrsConnectorTracingRegistry='Registry::HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\MSCRMBidsExtensions'
    #$dataMigrationTracingRegistry='Registry::HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\DATA MIGRATION WIZARD'
 
    # Ensure that the plugin is available prior to proceeding
    try{
        Add-PSSnapin Microsoft.Crm.PowerShell -ErrorAction Stop
    }catch{
        Write-Error $_
        return $false       
        }
 
    try{
        $callStack=$enable
        $setting = Get-CrmSetting TraceSettings
        if($enable){$setting.Enabled=$enable}
        if($callStack){$setting.CallStack=$callStack}
        if($categories){$setting.Categories=$categories}
        if($traceDirectory){
            if(!(test-path $traceDirectory)){mkdir $traceDirectory -Force}
            $setting.Directory=$traceDirectory
            }
        if($fileSize){$setting.FileSize=$fileSize}
        Set-CrmSetting $setting
        $currentTraceDirectory=(Get-ItemProperty -path $serverTracingRegistry -name TraceDirectory -ErrorAction Ignore).TraceDirectory
        if($traceDirectory -and ($currentTraceDirectory -ne $traceDirectory)){
            Set-Itemproperty -path $serverTracingRegistry -Name 'TraceDirectory' -value $traceDirectory
            }
        if($enable){
            Set-Itemproperty -path $serverTracingRegistry -Name 'Enabled' -value 1
            Write-OutPut "The Crm Tracing is now ENABLED"
        }else{
            Set-Itemproperty -path $serverTracingRegistry -Name 'Enabled' -value 0
            Write-OutPut "The Crm Tracing is now DISABLED"           
        }
        if($verbose){write-host "$((Get-CrmSetting TraceSettings|out-string).trim())"}
        if($resetIis){
            write-host "Now resetting Internet Information Service..."
            $null=iisreset
        }else{
            Write-Warning 'IISReset must be invoked for changes to become fully effective.'
            }
        return $true
    }catch{
        Write-Warning $_
        return $false
        }
}
 
setCrmTrace $enable $traceDirectory $categories $fileSize $resetIis

Troubleshooting:

Error message from Event Viewer:

Invalid Trace Directory. Additional Info:[ Invalid Trace Directory (Reporting Process:Trace Diretory is Null. TraceSettings: {Filename: ,FileCountSuffix:1 ,TraceFileSize:10485760 ,TraceDirectory: ,TracingCallStack:No ,IsTracingOff:Yes ,LoadState:LoadSuccessfulUnreported ,RefreshTraceInt:-1 ,SiteWideRefreshTraceInt:-2 ,RegistryRefreshTraceInt:-1 ,Precedence:Default} ] , AppDomain:CrmAsyncService)

Two registry keys at the path ‘HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\MSCRM’ must be set following the Set-CrmSetting command:

  1. Enabled: 0 or 1
  2. TraceDirectory: valid directory path required

 

Leave a Reply

Your email address will not be published. Required fields are marked *