Skip to content

Commit

Permalink
Merge branch 'dev' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
EsOsO committed Feb 11, 2021
2 parents c8f23b3 + b0e054b commit 2f27bb8
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 157 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.gitconfig
BuildOutput/
*.xml
debug.log
14 changes: 7 additions & 7 deletions Logging/public/Write-Log.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ Function Write-Log {
}

End {
[string] $messageText = $Message

if ($PSBoundParameters.ContainsKey('Arguments')) {
$messageText = $messageText -f $Arguments
}

$levelNumber = Get-LevelNumber -Level $PSBoundParameters.Level
$invocationInfo = (Get-PSCallStack)[$Script:Logging.CallerScope]

Expand All @@ -90,12 +84,18 @@ Function Write-Log {
pathname = $invocationInfo.ScriptName
filename = $fileName
caller = $invocationInfo.Command
message = $messageText
message = [string] $Message
rawmessage = [string] $Message
body = $Body
execinfo = $ExceptionInfo
pid = $PID
}

if ($PSBoundParameters.ContainsKey('Arguments')) {
$logMessage["message"] = [string] $Message -f $Arguments
$logMessage["args"] = $Arguments
}

#This variable is initiated via Start-LoggingManager
$Script:LoggingEventQueue.Add($logMessage)
}
Expand Down
33 changes: 18 additions & 15 deletions Logging/targets/Console.ps1
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
@{
Name = 'Console'
Description = 'Writes messages to console with different colors.'
Name = 'Console'
Description = 'Writes messages to console with different colors.'
Configuration = @{
Level = @{Required = $false; Type = [string]; Default = $Logging.Level}
Format = @{Required = $false; Type = [string]; Default = $Logging.Format}
ColorMapping = @{Required = $false; Type = [hashtable]; Default = @{
'DEBUG' = 'Blue'
'INFO' = 'Green'
'WARNING' = 'Yellow'
'ERROR' = 'Red'
}
Level = @{Required = $false; Type = [string]; Default = $Logging.Level }
Format = @{Required = $false; Type = [string]; Default = $Logging.Format }
PrintException = @{Required = $false; Type = [bool]; Default = $true }
ColorMapping = @{Required = $false; Type = [hashtable]; Default = @{
'DEBUG' = 'Blue'
'INFO' = 'Green'
'WARNING' = 'Yellow'
'ERROR' = 'Red'
}
}
}
Init = {
Init = {
param(
[hashtable] $Configuration
)
Expand All @@ -26,7 +27,7 @@
}
}
}
Logger = {
Logger = {
param(
[hashtable] $Log,
[hashtable] $Configuration
Expand All @@ -35,16 +36,18 @@
try {
$logText = Replace-Token -String $Configuration.Format -Source $Log

if (![String]::IsNullOrWhiteSpace($Log.ExecInfo)) {
$logText += "`n" + $Log.ExecInfo.InvocationInfo.PositionMessage
if (![String]::IsNullOrWhiteSpace($Log.ExecInfo) -and $Configuration.PrintException) {
$logText += "`n{0}" -f $Log.ExecInfo.Exception.Message
$logText += "`n{0}" -f (($Log.ExecInfo.ScriptStackTrace -split "`r`n" | %{"`t{0}" -f $_}) -join "`n")
}

$mtx = New-Object System.Threading.Mutex($false, 'ConsoleMtx')
[void] $mtx.WaitOne()

if ($Configuration.ColorMapping.ContainsKey($Log.Level)) {
$ParentHost.UI.WriteLine($Configuration.ColorMapping[$Log.Level], $ParentHost.UI.RawUI.BackgroundColor, $logText)
} else {
}
else {
$ParentHost.UI.WriteLine($logText)
}

Expand Down
47 changes: 29 additions & 18 deletions Logging/targets/Email.ps1
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
@{
Name = 'Email'
Description = 'Send log message to email recipients'
Name = 'Email'
Description = 'Send log message to email recipients'
Configuration = @{
SMTPServer = @{Required = $true; Type = [string]; Default = $null}
From = @{Required = $true; Type = [string]; Default = $null}
To = @{Required = $true; Type = [string]; Default = $null}
Subject = @{Required = $false; Type = [string]; Default = '[%{level:-7}] %{message}'}
Credential = @{Required = $false; Type = [pscredential]; Default = $null}
Level = @{Required = $false; Type = [string]; Default = $Logging.Level}
Port = @{Required = $false; Type = [int]; Default = 25}
UseSsl = @{Required = $false; Type = [bool]; Default = $false}
Format = @{Required = $false; Type = [string]; Default = $Logging.Format}
SMTPServer = @{Required = $true; Type = [string]; Default = $null }
From = @{Required = $true; Type = [string]; Default = $null }
To = @{Required = $true; Type = [string]; Default = $null }
Subject = @{Required = $false; Type = [string]; Default = '[%{level:-7}] %{message}' }
Credential = @{Required = $false; Type = [pscredential]; Default = $null }
Level = @{Required = $false; Type = [string]; Default = $Logging.Level }
Port = @{Required = $false; Type = [int]; Default = 25 }
UseSsl = @{Required = $false; Type = [bool]; Default = $false }
Format = @{Required = $false; Type = [string]; Default = $Logging.Format }
PrintException = @{Required = $false; Type = [bool]; Default = $false }
}
Logger = {
Logger = {
param(
[hashtable] $Log,
[hashtable] $Configuration
)

$Body = '<h3>{0}</h3>' -f $Log.Message

if (![String]::IsNullOrWhiteSpace($Log.ExecInfo)) {
$Body += '<pre>'
$Body += "`n{0}" -f $Log.ExecInfo.Exception.Message
$Body += "`n{0}" -f (($Log.ExecInfo.ScriptStackTrace -split "`r`n" | % { "`t{0}" -f $_ }) -join "`n")
$Body += '</pre>'
}

$Params = @{
SmtpServer = $Configuration.SMTPServer
From = $Configuration.From
To = $Configuration.To.Split(',').Trim()
Port = $Configuration.Port
UseSsl = $Configuration.UseSsl
Subject = Replace-Token -String $Configuration.Subject -Source $Log
Body = Replace-Token -String $Configuration.Format -Source $Log
From = $Configuration.From
To = $Configuration.To.Split(',').Trim()
Port = $Configuration.Port
UseSsl = $Configuration.UseSsl
Subject = Replace-Token -String $Configuration.Subject -Source $Log
Body = $Body
BodyAsHtml = $true
}

if ($Configuration.Credential) {
Expand Down
31 changes: 19 additions & 12 deletions Logging/targets/File.ps1
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
@{
Name = 'File'
Name = 'File'
Configuration = @{
Path = @{Required = $true; Type = [string]; Default = $null}
PrintBody = @{Required = $false; Type = [bool]; Default = $false}
Append = @{Required = $false; Type = [bool]; Default = $true}
Encoding = @{Required = $false; Type = [string]; Default = 'ascii'}
Level = @{Required = $false; Type = [string]; Default = $Logging.Level}
Format = @{Required = $false; Type = [string]; Default = $Logging.Format}
Path = @{Required = $true; Type = [string]; Default = $null }
PrintBody = @{Required = $false; Type = [bool]; Default = $false }
PrintException = @{Required = $false; Type = [bool]; Default = $false }
Append = @{Required = $false; Type = [bool]; Default = $true }
Encoding = @{Required = $false; Type = [string]; Default = 'ascii' }
Level = @{Required = $false; Type = [string]; Default = $Logging.Level }
Format = @{Required = $false; Type = [string]; Default = $Logging.Format }
}

Logger = {
Logger = {
param(
[hashtable] $Log,
[hashtable] $Configuration
)

if ($Configuration.PrintBody -and $Log.Body) {
$Log.Body = $Log.Body | ConvertTo-Json -Compress
} elseif (-not $Configuration.PrintBody -and $Log.Body) {
}
elseif (-not $Configuration.PrintBody -and $Log.Body) {
$Log.Remove('Body')
}

$Text = Replace-Token -String $Configuration.Format -Source $Log

if (![String]::IsNullOrWhiteSpace($Log.ExecInfo) -and $Configuration.PrintException) {
$Text += "`n{0}" -f $Log.ExecInfo.Exception.Message
$Text += "`n{0}" -f (($Log.ExecInfo.ScriptStackTrace -split "`r`n" | % { "`t{0}" -f $_ }) -join "`n")
}

$Params = @{
Append = $Configuration.Append
FilePath = Replace-Token -String $Configuration.Path -Source $Log
Encoding = $Configuration.Encoding
Append = $Configuration.Append
FilePath = Replace-Token -String $Configuration.Path -Source $Log
Encoding = $Configuration.Encoding
}

$Text | Out-File @Params
Expand Down
35 changes: 20 additions & 15 deletions Logging/targets/Seq.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,37 @@
Name = 'Seq'
Description = 'Sends log data to the designated Seq server web service'
Configuration = @{
Url = @{Required = $true; Type = [string]; Default = $null}
ApiKey = @{Required = $false; Type = [string]; Default = $null}
Properties = @{Required = $true; Type = [hashtable]; Default = $null}
Level = @{Required = $false; Type = [string]; Default = $Logging.Level}
Url = @{Required = $true; Type = [string]; Default = $null }
ApiKey = @{Required = $false; Type = [string]; Default = $null }
Properties = @{Required = $false; Type = [hashtable]; Default = $null }
Level = @{Required = $false; Type = [string]; Default = $Logging.Level }
}
Logger = {
Logger = {
param(
[hashtable] $Log,
[hashtable] $Configuration
)

$Body = @{
Events = @(@{
Timestamp = [System.DateTimeOffset]::Now.ToString('o')
Level = $Configuration.Level
MessageTemplate = $Log.Message | ConvertTo-Json
Properties = ($Log + $Configuration.Properties) | ConvertTo-Json
})
"@t" = $Log.TimestampUtc
"@l" = $Configuration.Level
"@m" = $Log.Message
"@mt" = $Log.RawMessage
}

if ($Log.ExecInfo) {
$Body["@x"] = $Log.ExecInfo.ScriptStackTrace
}

$Body += $Log

if ($Configuration.ApiKey) {
$Url = '{0}/api/events/raw?apiKey={1}' -f $Configuration.Url, $Configuration.ApiKey
} else {
$Url = '{0}/api/events/raw?' -f $Configuration.Url
$Url = '{0}/api/events/raw?clef&apiKey={1}' -f $Configuration.Url, $Configuration.ApiKey
}
else {
$Url = '{0}/api/events/raw?clef' -f $Configuration.Url
}

Invoke-RestMethod -Uri $Url -Body ($Body | ConvertTo-Json) -ContentType "application/json" -Method POST | Out-Null
Invoke-RestMethod -Uri $Url -Body ($Body | ConvertTo-Json -Compress) -Method POST | Out-Null
}
}
Loading

0 comments on commit 2f27bb8

Please sign in to comment.