-
-
Notifications
You must be signed in to change notification settings - Fork 241
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 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 7cc3bb1
Merge remote-tracking branch 'origin/main' into feat/logs-log4net
jamescrosswell 441b655
fix(log4net): guard null RenderedMessage and clean up ThreadContext i…
jamescrosswell 0a7f388
test(log4net): simplify structured log assertions with Sentry.Testing…
jamescrosswell 575d2e6
feat(log4net): map LoggerName to category.name on structured logs
jamescrosswell 56abb51
chore(log4net): drop redundant NETFRAMEWORK guard in sample
jamescrosswell b919a28
refactor(logs): inline SentryLog.Create instead of a separate partial…
jamescrosswell 1f3468c
refactor(log4net): flatten CaptureStructuredLog with an early return
jamescrosswell c5d512d
fix(log4net): guard against null GetProperties() in structured logging
jamescrosswell 13260a4
Refactor Appender to avoid a huge Append method
jamescrosswell 2b31534
fix(log4net): honor appender settings and don't skip logs below Minim…
jamescrosswell 4236d5f
Update src/Sentry.Log4Net/SentryAppender.cs
jamescrosswell 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
Some comments aren't visible on the classic Files Changed page.
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,57 @@ | ||
| namespace Sentry.Log4Net; | ||
|
|
||
| public partial class SentryAppender | ||
| { | ||
| private static void CaptureStructuredLog(IHub hub, SentryOptions options, LoggingEvent loggingEvent, string? environment, bool sendIdentity) | ||
| { | ||
| if (loggingEvent.ToSentryLogLevel() is not { } level) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| 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 message = !string.IsNullOrWhiteSpace(loggingEvent.RenderedMessage) ? loggingEvent.RenderedMessage : string.Empty; | ||
| var log = SentryLog.Create(hub, timestamp, level, message, template, parameters); | ||
|
|
||
| var scope = hub.GetScope(); | ||
| log.SetDefaultAttributes(options, scope, Sdk); | ||
| log.SetOrigin("auto.log.log4net"); | ||
|
|
||
| // Honor the appender-level settings, overriding the scope/options defaults, to match the SentryEvent path. | ||
| if (!string.IsNullOrWhiteSpace(environment)) | ||
| { | ||
| log.SetAttribute("sentry.environment", environment!); | ||
| } | ||
|
|
||
| if (sendIdentity && !string.IsNullOrEmpty(loggingEvent.Identity)) | ||
| { | ||
| log.SetAttribute("user.id", loggingEvent.Identity); | ||
| } | ||
|
|
||
| if (loggingEvent.LoggerName is { } loggerName) | ||
| { | ||
| log.SetAttribute("category.name", loggerName); | ||
| } | ||
|
|
||
| // GetProperties() is non-null in current log4net, but the sibling GetLoggingEventProperties guards | ||
| // against null, so we do too rather than rely on log4net's internals. | ||
| if (loggingEvent.GetProperties() is { } properties) | ||
| { | ||
| foreach (var property in properties) | ||
| { | ||
| 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); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| 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
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.
Uh oh!
There was an error while loading. Please reload this page.