Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ pwsh ./dependencies/download.ps1
Then you can run the sample, for example:
```sh
pwsh ./samples/locate-city.ps1 Toronto
```

Or send structured logs to Sentry (see the [Sentry Logs docs](https://docs.sentry.io/platforms/dotnet/logs/)):
```sh
pwsh ./samples/send-logs.ps1
```
50 changes: 50 additions & 0 deletions samples/send-logs.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<#
.SYNOPSIS
Demonstrates sending structured logs to Sentry from PowerShell.
.DESCRIPTION
Shows how to enable Sentry Logs (https://docs.sentry.io/platforms/dotnet/logs/)
in the PowerShell module and emit log messages at various severity levels,
including templated messages with structured parameters.
.EXAMPLE
PS> ./send-logs.ps1
.LINK
https://docs.sentry.io/platforms/dotnet/logs/
#>

# Import the Sentry module. In your code, you would just use `Import-Module Sentry`.
Import-Module $PSScriptRoot/../modules/Sentry/Sentry.psd1

# Start the Sentry client. Set Experimental.EnableLogs = $true to opt in to Logs.
Start-Sentry {
$_.Dsn = 'https://997874440feaba4ecc65c1e25df7912b@o447951.ingest.us.sentry.io/4508073336176640'
$_.Debug = $true
$_.Experimental.EnableLogs = $true
}

try {
# The logger lives on SentrySdk. Each level maps to a separate method.
[Sentry.SentrySdk]::Logger.LogTrace('Trace from PowerShell')
[Sentry.SentrySdk]::Logger.LogDebug('Debug from PowerShell')
[Sentry.SentrySdk]::Logger.LogInfo('Info from PowerShell')
[Sentry.SentrySdk]::Logger.LogWarning('Warning from PowerShell')
[Sentry.SentrySdk]::Logger.LogError('Error from PowerShell')
[Sentry.SentrySdk]::Logger.LogFatal('Fatal from PowerShell')

# Templated messages: placeholders in the template become structured
# attributes on the log record, so you can search/filter on them in Sentry.
$user = $env:USER ?? $env:USERNAME
Comment thread
vaind marked this conversation as resolved.
$host_ = [System.Net.Dns]::GetHostName()
$psVersion = $PSVersionTable.PSVersion.ToString()
# Templates use positional placeholders ({0}, {1}, ...) and the parameters are
# captured as structured attributes on the log record so you can search/filter
# on them in Sentry. The .NET method signature uses `params object[]`, which
# PowerShell doesn't auto-expand — wrap arguments in [object[]].
[Sentry.SentrySdk]::Logger.LogInfo(
'User {0} ran send-logs.ps1 on {1} (PowerShell {2})',
([object[]]@($user, $host_, $psVersion)))
Comment on lines +42 to +44
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cases such as this - if we want to add the sample for - are worth a separate native PowerShell API

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sth like this for example: #131


# Logs are buffered and flushed in the background. Give them a moment to send.
[Sentry.SentrySdk]::Flush([TimeSpan]::FromSeconds(5)) | Out-Null
} finally {
Stop-Sentry
}
Loading