Last active
November 8, 2019 11:03
-
-
Save nblumhardt/7120fc008b725fa4a26e to your computer and use it in GitHub Desktop.
Seq from Powershell POC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module Seq | |
# Specify the Seq server URL; an -apiKey can also be provided. | |
# Any properties set here will be attached to all events. | |
$seq = Open-Seq "http://my-seq" -properties @{ Machine = $env:ComputerName } | |
# Simple information method | |
Send-SeqEvent $seq "Hello from PowerShell" | |
# Specify additional properties and a level value (Verbose, Debug, Warning, Error and Fatal are allowed) | |
Send-SeqEvent $seq "Something is broken!" -level Error -properties @{ User = $env:Username } | |
# Use the -template switch if the message is a Serilog-style template | |
Send-SeqEvent $seq "Leonard is {Age}" -properties @{ Age = 42 } -template |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Open-Seq ([string] $url, [string] $apiKey, $properties = @{}) | |
{ | |
return @{ Url = $url; ApiKey = $apiKey; Properties = $properties.Clone() } | |
} | |
function Send-SeqEvent ( | |
$seq, | |
[string] $text, | |
[string] $level, | |
$properties = @{}, | |
[switch] $template) | |
{ | |
if (-not $level) | |
{ | |
$level = 'Information' | |
} | |
if (@('Verbose', 'Debug', 'Information', 'Warning', 'Error', 'Fatal') -notcontains $level) | |
{ | |
$level = 'Information' | |
} | |
$allProperties = $seq["Properties"].Clone() | |
$allProperties += $properties | |
$messageTemplate = "{Text}" | |
if ($template) | |
{ | |
$messageTemplate = $text; | |
} | |
else | |
{ | |
$allProperties += @{ Text = $text; } | |
} | |
$body = "{""Events"": [ { | |
""Timestamp"": ""$([System.DateTimeOffset]::Now.ToString('o'))"", | |
""Level"": ""$level"", | |
""MessageTemplate"": $($messageTemplate | ConvertTo-Json), | |
""Properties"": $($allProperties | ConvertTo-Json) }]}" | |
$target = "$($seq["Url"])/api/events/raw?apiKey=$($seq["ApiKey"])" | |
Invoke-RestMethod -Uri $target -Body $body -ContentType "application/json" -Method POST | |
} |
Awesome :) --- just added it to the Seq docs: https://getseq.atlassian.net/wiki/display/SEQ10/Writing+events+from+PowerShell --- patches/enhancements accepted!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I actually really like this. I might use this in something i was working on! :)