Seq - Log Deployment

Octopus.Script exported 2023-02-16 by harrisonmeister belongs to ‘Seq’ category.

Post details of the deployment to a Seq log server. Add this as the last step in a process, and ensure it is set to run always (on success and failure).

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Seq server URL

SeqServerUrl = http://localhost:5341

The URL of the Seq server.

Seq API key

SeqApiKey

If an API key is required for writing events, specify it here.

Script body

Steps based on this template will execute the following PowerShell script.

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 = @{},
    [string] $exception = $null,
    [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; }
  }
  
  $exceptionProperty = ""
  if ($exception) {
      $exceptionProperty = """Exception"": $($exception | ConvertTo-Json),"
  }
  
  $body = "{""Events"": [ {
    ""Timestamp"": ""$([System.DateTimeOffset]::Now.ToString('o'))"",
    ""Level"": ""$level"",
    $exceptionProperty
    ""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
}

Write-Output "Logging the deployment result to Seq at $SeqServerUrl..."

$seq = Open-Seq $SeqServerUrl -apiKey $SeqApiKey

$properties = @{
    ProjectName = $OctopusParameters['Octopus.Project.Name'];
    ReleaseNumber = $OctopusParameters['Octopus.Release.Number'];
    Result = "succeeded";
    EnvironmentName = $OctopusParameters['Octopus.Environment.Name'];
    DeploymentName = $OctopusParameters['Octopus.Deployment.Name'];
    Channel = $OctopusParameters['Octopus.Release.Channel.Name'];
    DeploymentLink = $OctopusParameters['#{if Octopus.Web.ServerUri}Octopus.Web.ServerUri#{else}Octopus.Web.BaseUrl#{/if}'] + $OctopusParameters['Octopus.Web.DeploymentLink']
}

$level = "Information"
$exception = $null
if ($OctopusParameters['Octopus.Deployment.Error']) {
    $level = "Error"
    $properties["Result"] = "failed"
    $properties["Error"] = $OctopusParameters['Octopus.Deployment.Error']
    $exception = $OctopusParameters['Octopus.Deployment.ErrorDetail']
}

try {
    Send-SeqEvent $seq "A deployment of {ProjectName} release {ReleaseNumber} {Result} in {EnvironmentName}" -level $level -template -properties $properties -exception $exception
} catch [Exception] {
    [System.Console]::Error.WriteLine("Unable to write deployment details to Seq")
    $_.Exception | format-list -force
}

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "a55a825b-8e8b-4995-8143-0f9dd7b6bcfa",
  "Name": "Seq - Log Deployment",
  "Description": "Post details of the deployment to a [Seq](https://getseq.net) log server. Add this as the last step in a process, and ensure it is set to run always (on success and failure).",
  "Version": 22,
  "ExportedAt": "2023-02-16T15:38:44.043Z",
  "ActionType": "Octopus.Script",
  "Author": "harrisonmeister",
  "Parameters": [
    {
      "Name": "SeqServerUrl",
      "Label": "Seq server URL",
      "HelpText": "The URL of the Seq server.",
      "DefaultValue": "http://localhost:5341",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "SeqApiKey",
      "Label": "Seq API key",
      "HelpText": "If an [API key](http://docs.getseq.net/docs/api-keys) is required for writing events, specify it here.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "function Open-Seq ([string] $url, [string] $apiKey, $properties = @{})\n{\n  return @{ Url = $url; ApiKey = $apiKey; Properties = $properties.Clone() }\n}\n  \nfunction Send-SeqEvent (\n    $seq,\n    [string] $text,\n    [string] $level,\n    $properties = @{},\n    [string] $exception = $null,\n    [switch] $template)\n{\n  if (-not $level) {\n    $level = 'Information'\n  }\n   \n  if (@('Verbose', 'Debug', 'Information', 'Warning', 'Error', 'Fatal') -notcontains $level) {\n    $level = 'Information'\n  }\n  \n  $allProperties = $seq[\"Properties\"].Clone()\n  $allProperties += $properties\n  \n  $messageTemplate = \"{Text}\"\n  \n  if ($template) {\n    $messageTemplate = $text;\n  } else {\n    $allProperties += @{ Text = $text; }\n  }\n  \n  $exceptionProperty = \"\"\n  if ($exception) {\n      $exceptionProperty = \"\"\"Exception\"\": $($exception | ConvertTo-Json),\"\n  }\n  \n  $body = \"{\"\"Events\"\": [ {\n    \"\"Timestamp\"\": \"\"$([System.DateTimeOffset]::Now.ToString('o'))\"\",\n    \"\"Level\"\": \"\"$level\"\",\n    $exceptionProperty\n    \"\"MessageTemplate\"\": $($messageTemplate | ConvertTo-Json),\n    \"\"Properties\"\": $($allProperties | ConvertTo-Json) }]}\"\n  \n  $target = \"$($seq[\"Url\"])/api/events/raw?apiKey=$($seq[\"ApiKey\"])\"\n  \n  Invoke-RestMethod -Uri $target -Body $body -ContentType \"application/json\" -Method POST\n}\n\nWrite-Output \"Logging the deployment result to Seq at $SeqServerUrl...\"\n\n$seq = Open-Seq $SeqServerUrl -apiKey $SeqApiKey\n\n$properties = @{\n    ProjectName = $OctopusParameters['Octopus.Project.Name'];\n    ReleaseNumber = $OctopusParameters['Octopus.Release.Number'];\n    Result = \"succeeded\";\n    EnvironmentName = $OctopusParameters['Octopus.Environment.Name'];\n    DeploymentName = $OctopusParameters['Octopus.Deployment.Name'];\n    Channel = $OctopusParameters['Octopus.Release.Channel.Name'];\n    DeploymentLink = $OctopusParameters['#{if Octopus.Web.ServerUri}Octopus.Web.ServerUri#{else}Octopus.Web.BaseUrl#{/if}'] + $OctopusParameters['Octopus.Web.DeploymentLink']\n}\n\n$level = \"Information\"\n$exception = $null\nif ($OctopusParameters['Octopus.Deployment.Error']) {\n    $level = \"Error\"\n    $properties[\"Result\"] = \"failed\"\n    $properties[\"Error\"] = $OctopusParameters['Octopus.Deployment.Error']\n    $exception = $OctopusParameters['Octopus.Deployment.ErrorDetail']\n}\n\ntry {\n    Send-SeqEvent $seq \"A deployment of {ProjectName} release {ReleaseNumber} {Result} in {EnvironmentName}\" -level $level -template -properties $properties -exception $exception\n} catch [Exception] {\n    [System.Console]::Error.WriteLine(\"Unable to write deployment details to Seq\")\n    $_.Exception | format-list -force\n}\n"
  },
  "Category": "Seq",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/seq-log-deployment.json",
  "Website": "/step-templates/a55a825b-8e8b-4995-8143-0f9dd7b6bcfa",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALewAAC3sBSRnwgAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAAWdEVYdFRpdGxlAFNlcV9EaWFtb25kLU1haW7nqGGXAAAMQklEQVR4nO3d63PU5R0F8PPs5sYliRAguwsEhVpp66UXQBEFFJ22U1sLaNV2rNZpmbZj33WkM9qOtdTWTvuil2k7tCPWUQgbCKjIdBSSgECFZDcExQuRi4Lg7m+TkM11yf726QsQBWMgm939/p79nc9fcN4cnu+Z3bAAEREREREREREREREREREREREREREREREREREREREA4FGtPUEd9ErnyDWPdAByvuesHaWft8LrYF06UTpLrrEgNKRqa+9nE3rUbgCLpbNIYEHoU9VYods82t4D4HPSWaSwIPQJWmu1NhperjWeA1AunUdSgXQAcpZn2l4tq4mFnlZQt0tncQIWhM4KWo1XwPZsBDBTOotT8MQiAEDQCn8L2rMHLMc5WBCX+3BvQOuNAMqk8zgNTywXC3buGl9jhVcr4KvSWZyKBXGpdZ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