Documentation
DocumentationDiscussions
These docs are for v2. Click to read the latest docs for v2024.1.

Using Powershell

Windows PowerShell is not a primary client for Seq, however it can be useful to write events in some scenarios. For this purpose a sample PowerShell module Seq is provided inline below.

Using the Seq module

With the Seq.psm1 module in the same folder as the script, the example below shows how events are written:

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
If the module is in the module path replace the first line with Import-Module Seq.
The Seq module

The Seq module is provided below, and is licensed under the Apache 2.0 License. Patches/enhancements are gratefully accepted via the Seq GitHub tracker.

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
}