-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWrite-Exception.ps1
More file actions
59 lines (48 loc) · 2.16 KB
/
Write-Exception.ps1
File metadata and controls
59 lines (48 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<#
.SYNOPSIS
Write the exception logs in the exception log file.
.DESCRIPTION
Write the exception logs in the exception log file. When throws an exception in the system we are handling the write the exception message and stack trace in the exception log file. which is generating based on the current date.
.PARAMETER ErrorObj
Optional, Stack trace of the exception.
.PARAMETER Message
Optional, Custom string message.
.PARAMETER Stop
Default $false, If $true execution will be stopped.
.OUTPUTS
Write the exception into the exception file. File generate at '/Uploads/Logs/Exception_{CurrentDate}.txt'
.EXAMPLE
Write-Exception -ErrorObj $_ -Message "Scripts will be stop execution" -Stop $true
Write-Exception -ErrorObj $_ -Message "Scripts will be contniue after logging the exception details" -Stop $false
#>
function Write-Exception() {
[cmdletbinding()]
Param (
$ErrorObj,
$Message,
[bool]$Stop = $false
)
Begin {
}
Process {
$currentDate = (Get-Date).ToString($Global:Resources.DateFormat);
$errorLogFilePath = Create-File -FolderPath "$($Global:RootPath+$Global:Resources.LogPath)" -FileName "Exception_$currentDate.txt"
#Check this condition because when file not exists and create new one, It's returning Array so needs to select last-one
if ($errorLogFilePath.GetType().Name -ne 'String') {
$errorLogFilePath = Get-LastOrDefault $errorLogFilePath
}
$separator = "------------------------------$(Get-Date)---Stopped: $Stop------------------------------"
$separator | Out-File $errorLogFilePath -Append -Encoding oem
$Message | Out-File $errorLogFilePath -Append -Encoding oem
if ($null -ne $ErrorObj) {
"Error Message: " + $ErrorObj.Exception.Message | Out-File $errorLogFilePath -Append -Encoding oem
$ErrorObj[0] | Format-List -Force | Out-File $errorLogFilePath -Append -Encoding oem
}
Write-Information -Message $Message -InformationAction Continue
if ($Stop) {
EXIT
}
}
End {
}
}