Skip to content

How can I log errors from my psake script to a file?

jmatos edited this page Sep 13, 2010 · 10 revisions

psake exports a hashtable variable named $psake to the global namespace or scope. Within the $psake hashtable is a key named log_error which is defaulted to $false. You can log any errors from your build to a file by setting log_error to a value of $true.
You do this right after you import the psake.psm1 module and before you run your build script.

PS > helloworld\Build> Import-Module .\psake.psm1
PS > helloworld\Build> $psake

Name                           Value
----                           -----
version                        4.00
build_script_file
use_exit_on_error              False
build_success                  False
log_error                      False
framework_version
default_build_file_name        default.ps1

Here is a helper script that will take care of setting the log_error variable for you

$scriptPath = Split-Path $MyInvocation.InvocationName
Import-Module (join-path $scriptPath psake.psm1)
$psake.log_error = $true
invoke-psake -framework '4.0'

If you run the following script you should get an error message to the console and an error file is created in the directory where you ran your script.

Task default -Depends ShouldThrowError 

Task ShouldThrowError {
	throw "This is a test"
}

Console Output:

PS > helloworld\Build> Invoke-psake .\LogError.ps1
Executing task: ShouldThrowError
LogError.ps1: This is a test

The file will be named with the following pattern: psake-error-log-YYYYMMDD.log
The contents of the file will look like the following:

----------------------------------------------------------------------
4/25/2010 2:36:21 PM: An Error Occurred. See Error Details Below: 
----------------------------------------------------------------------
ErrorRecord
PSMessageDetails      : 
Exception             : System.Management.Automation.RuntimeException: This is a test
TargetObject          : This is a test
CategoryInfo          : OperationStopped: (This is a test:String) [], RuntimeException
FullyQualifiedErrorId : This is a test
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
PipelineIterationInfo : {}

ErrorRecord.InvocationInfo
MyCommand        : 
BoundParameters  : {}
UnboundArguments : {}
ScriptLineNumber : 4
OffsetInLine     : 7
HistoryId        : 34
ScriptName       : C:\Users\Daddy\Documents\Projects\helloworld\Build\LogError.ps1
Line             :     throw "This is a test"
PositionMessage  : 
                   At C:\Users\Daddy\Documents\Projects\helloworld\Build\LogError.ps1:4 char:7
                   +     throw <<<<  "This is a test"
InvocationName   : throw
PipelineLength   : 0
PipelinePosition : 0
ExpectingInput   : False
CommandOrigin    : Internal

Exception
0000000000000000000000000000000000000000000000000000000000000000000000
ErrorRecord                 : This is a test
StackTrace                  : 
WasThrownFromThrowStatement : True
Message                     : This is a test
Data                        : {}
InnerException              : 
TargetSite                  : 
HelpLink                    : 
Source                      : 

----------------------------------------------------------------------
Script Variables
----------------------------------------------------------------------

Name                           Value                                                                                                         
----                           -----                                                                                                         
_                                                                                                                                            
args                           {}                                                                                                            
context                        {System.Collections.Hashtable}                                                                                
Error                          {}                                                                                                            
false                          False                                                                                                         
input                          System.Collections.ArrayList+ArrayListEnumeratorSimple                                                        
MaximumAliasCount              4096                                                                                                          
MaximumDriveCount              4096                                                                                                          
MaximumErrorCount              256                                                                                                           
MaximumFunctionCount           4096                                                                                                          
MaximumVariableCount           4096                                                                                                          
MyInvocation                   System.Management.Automation.InvocationInfo                                                                   
null                                                                                                                                         
psake                          {build_script_file, version, default_build_file_name, use_exit_on_error...}                                   
this                                                                                                                                         
true                           True                                                                                                          
Clone this wiki locally