-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add structured logging sample #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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://[email protected]/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 | ||
| $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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sth like this for example: #131
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. k, we may as well close this PR as a duplicate of that superior solution then (#131 also includes a sample, which renders this redundant). |
||
|
|
||
| # 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 | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.