Skip to content

Instantly share code, notes, and snippets.

@nblumhardt
Last active November 8, 2019 11:03
Show Gist options
  • Save nblumhardt/7120fc008b725fa4a26e to your computer and use it in GitHub Desktop.
Save nblumhardt/7120fc008b725fa4a26e to your computer and use it in GitHub Desktop.
Seq from Powershell POC
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
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
}
@BrendanThompson
Copy link

I actually really like this. I might use this in something i was working on! :)

@nblumhardt
Copy link
Author

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