-
-
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
Open
jamescrosswell
wants to merge
1
commit into
main
Choose a base branch
from
logging-sample
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+55
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| $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 |
||
|
|
||
| # 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 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.