-
-
Notifications
You must be signed in to change notification settings - Fork 235
feat(logs): add log4net integration
#5172
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
Draft
Flash0ver
wants to merge
7
commits into
main
Choose a base branch
from
feat/logs-log4net
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.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
76035cd
feat(logs): add `log4net` integration
Flash0ver 10916bf
test: fix
Flash0ver d5e3ab4
fix log4net sample app
Flash0ver d4e69a9
Format code
getsentry-bot 8b180f1
ref: move to partial class
Flash0ver 7a39c62
Merge branch 'main' into feat/logs-log4net
Flash0ver ca31e0b
test: add Properties-to-Attributes tests
Flash0ver 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
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
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,33 @@ | ||
| namespace Sentry.Log4Net; | ||
|
|
||
| public partial class SentryAppender | ||
| { | ||
| private static void CaptureStructuredLog(IHub hub, SentryOptions options, LoggingEvent loggingEvent) | ||
| { | ||
| var level = loggingEvent.ToSentryLogLevel(); | ||
| if (level.HasValue) | ||
| { | ||
| DateTimeOffset timestamp = new(loggingEvent.TimeStampUtc); | ||
| const string? template = null; // cannot get format-string from `log4net.Util.SystemStringFormat` via `LoggingEvent.MessageObject` | ||
| var parameters = ImmutableArray<KeyValuePair<string, object>>.Empty; // cannot get arguments from `log4net.Util.SystemStringFormat` via `LoggingEvent.MessageObject` | ||
|
|
||
| var log = SentryLog.Create(hub, timestamp, level.Value, loggingEvent.RenderedMessage, template, parameters); | ||
|
|
||
| log.SetDefaultAttributes(options, Sdk); | ||
| log.SetOrigin("auto.log.log4net"); | ||
|
|
||
| foreach (var property in loggingEvent.GetProperties()) | ||
| { | ||
| if (property is DictionaryEntry { Key: string key, Value: { } value }) | ||
| { | ||
| if (key.Length != 0 && !key.StartsWith("log4net:", StringComparison.OrdinalIgnoreCase) && !Guid.TryParse(key, out _)) | ||
| { | ||
| log.SetAttribute($"property.{key}", value); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| hub.Logger.CaptureLog(log); | ||
| } | ||
| } | ||
| } | ||
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
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,18 @@ | ||
| namespace Sentry; | ||
|
|
||
| public sealed partial class SentryLog | ||
| { | ||
| internal static SentryLog Create(IHub hub, DateTimeOffset timestamp, SentryLogLevel level, string message, string? template, ImmutableArray<KeyValuePair<string, object>> parameters) | ||
|
Check warning on line 5 in src/Sentry/SentryLog.Factory.cs
|
||
| { | ||
| hub.GetTraceIdAndSpanId(out var traceId, out var spanId); | ||
|
|
||
| SentryLog log = new(timestamp, traceId, level, message) | ||
| { | ||
| Template = template, | ||
| Parameters = parameters, | ||
| SpanId = spanId, | ||
| }; | ||
|
|
||
| return log; | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing null check for
RenderedMessagein structured loggingMedium Severity
loggingEvent.RenderedMessageis passed directly toSentryLog.Createas a non-nullablestring messageparameter, but it can return null in log4net (e.g., when no message object is set). The existing code in the same class defensively handles this — line 103 uses!string.IsNullOrWhiteSpace(loggingEvent.RenderedMessage)and falls back tostring.Empty. The new structured logging path lacks this guard, which could result in a null value flowing intoSentryLog.Messageand causing issues during serialization.Reviewed by Cursor Bugbot for commit ca31e0b. Configure here.