-
Notifications
You must be signed in to change notification settings - Fork 0
/
RabbitMqLogger.psm1
101 lines (90 loc) · 2.82 KB
/
RabbitMqLogger.psm1
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
$scriptPath = Split-Path -parent $PSCommandPath
Add-Type -Path "$scriptPath\RabbitMQ.Client.dll"
Add-Type -Path "$scriptPath\EasyNetQ.dll"
Add-Type -Path "$scriptPath\RabbitMqLogger.dll"
# Create convenience access to the logger and enum values in a global object
$global:rmq = @{}
$global:rmq.Info = [RabbitMqLogger.MessageType]::Info
$global:rmq.JobStart = [RabbitMqLogger.MessageType]::JobStart
$global:rmq.JobEnd = [RabbitMqLogger.MessageType]::JobEnd
$global:rmq.StepStart = [RabbitMqLogger.MessageType]::StepStart
$global:rmq.StepEnd = [RabbitMqLogger.MessageType]::StepEnd
function HandleException($exception) {
$inner = $exception.InnerException
while ($inner) {
$exception = $inner
$inner = $exception.InnerException
}
Write-Error "Could not log to RMQ: $($exception.Message)"
}
function New-RmqLogger($host, $topic = "GlobalLogging") {
$global:rmq.Logger = New-Object RabbitMqLogger.Logger -ArgumentList $host, $topic
}
function Assert-Logger() {
if (-Not $global:rmq.Logger) {
Write-Error "RMQ logger has not been initialized. Use New-RmqLogger."
}
}
function Write-DebugRmqLog(
[Parameter(Mandatory=$true)][string]$message,
[RabbitMqLogger.MessageType]$type = $global:rmq.Info,
[string]$category = "Uncategorized") {
Assert-Logger
try {
$global:rmq.Logger.Debug($message, $type, $category)
} catch {
HandleException $_.Exception
}
}
function Write-InfoRmqLog(
[Parameter(Mandatory=$true)][string]$message,
[RabbitMqLogger.MessageType]$type = $global:rmq.Info,
[string]$category = "Uncategorized") {
Assert-Logger
try {
$global:rmq.Logger.Info($message, $type, $category)
} catch {
HandleException $_.Exception
}
}
function Write-WarnRmqLog(
[Parameter(Mandatory=$true)][string]$message,
[RabbitMqLogger.MessageType]$type = $global:rmq.Info,
[string]$category = "Uncategorized") {
Assert-Logger
try {
$global:rmq.Logger.Warn([string]$message, $type, $category)
} catch {
HandleException $_.Exception
}
}
function Write-ErrorRmqLog(
[Parameter(Mandatory=$true)][string]$message,
[RabbitMqLogger.MessageType]$type = $global:rmq.Info,
[string]$category = "Uncategorized",
$error = $null) {
Assert-Logger
try {
$global:rmq.Logger.Error($message, $type, $category, $error)
} catch {
HandleException $_.Exception
}
}
function Write-FatalRmqLog(
[string]$message,
[RabbitMqLogger.MessageType]$type = $global:rmq.Info,
[string]$category = "Uncategorized",
$error = $null) {
Assert-Logger
try {
$global:rmq.Logger.Fatal($message, $type, $category, $error)
} catch {
HandleException $_.Exception
}
}
Export-ModuleMember -Function New-RmqLogger
Export-ModuleMember -Function Write-DebugRmqLog
Export-ModuleMember -Function Write-InfoRmqLog
Export-ModuleMember -Function Write-WarnRmqLog
Export-ModuleMember -Function Write-ErrorRmqLog
Export-ModuleMember -Function Write-FatalRmqLog