-
-
Notifications
You must be signed in to change notification settings - Fork 241
feat(logs): add NLog integration
#5176
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
96f2581
feat(logs): add `NLog` integration
Flash0ver de54732
chore: update NLog sample app
Flash0ver f558baf
Merge branch 'main' into feat/logs-nlog
Flash0ver 07330e7
fix(nlog): read EnableLogs from Hub options instead of target options
jamescrosswell ac37cef
refactor: inline SentryLog.Create into SentryLog.cs
jamescrosswell 7e572c9
test(nlog): simplify structured log attribute assertions
jamescrosswell e2f8cc4
fix(nlog): attach logger name to structured logs
jamescrosswell 5f3c30c
fix(nlog): give unnamed template holes positional parameter names
jamescrosswell 4a228d0
refactor(nlog): simplify HashSet capacity guard
jamescrosswell 4716b5c
Merge remote-tracking branch 'origin/main' into feat/logs-nlog
jamescrosswell 70e19bc
fix(nlog): pass Scope to SetDefaultAttributes on structured logs
jamescrosswell 35afe6f
refactor(nlog): flatten CaptureStructuredLog with a guard clause
jamescrosswell 2da7f13
refactor(nlog): flatten GetStructuredLoggingParametersAndAttributes
jamescrosswell 2b8c331
.
jamescrosswell 44a9552
Tweaked comments
jamescrosswell 9775861
fix(nlog): capture structured logs after events, guarded against fail…
jamescrosswell 842e133
Merge remote-tracking branch 'origin/feat/logs-nlog' into feat/logs-nlog
jamescrosswell b0fea90
Refactored InnerWrite - was getting a bit long
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
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,91 @@ | ||
| namespace Sentry.NLog; | ||
|
|
||
| public sealed partial class SentryTarget | ||
| { | ||
| private static void CaptureStructuredLog(IHub hub, SentryOptions options, LogEventInfo logEvent) | ||
| { | ||
| if (logEvent.Level.ToSentryLogLevel() is not { } level) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| DateTimeOffset timestamp = new(logEvent.TimeStamp); | ||
| GetStructuredLoggingParametersAndAttributes(logEvent, out var parameters, out var attributes); | ||
|
|
||
| var log = SentryLog.Create(hub, timestamp, level, logEvent.FormattedMessage, logEvent.Message, parameters); | ||
|
|
||
| var scope = hub.GetScope(); | ||
| log.SetDefaultAttributes(options, scope, Sdk); | ||
| log.SetOrigin("auto.log.nlog"); | ||
|
|
||
| if (logEvent.LoggerName is not null) | ||
| { | ||
| log.Attributes.SetAttribute("category.name", logEvent.LoggerName); | ||
| } | ||
|
|
||
| foreach (var attribute in attributes) | ||
| { | ||
| log.SetAttribute(attribute.Key, attribute.Value); | ||
| } | ||
|
|
||
| hub.Logger.CaptureLog(log); | ||
| } | ||
|
|
||
| private static void GetStructuredLoggingParametersAndAttributes(LogEventInfo logEvent, out ImmutableArray<KeyValuePair<string, object>> parameters, out List<KeyValuePair<string, object>> attributes) | ||
| { | ||
| parameters = GetParameters(logEvent, out var parameterNames); | ||
| attributes = []; | ||
|
|
||
| if (!logEvent.HasProperties) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| foreach (var property in logEvent.Properties) | ||
| { | ||
| if (property.Key is string key && !string.IsNullOrWhiteSpace(key) && | ||
| property.Value is { } value && | ||
| !parameterNames.Contains(key)) | ||
| { | ||
| attributes.Add(new KeyValuePair<string, object>($"property.{key}", value)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static ImmutableArray<KeyValuePair<string, object>> GetParameters(LogEventInfo logEvent, out HashSet<string> parameterNames) | ||
| { | ||
| var parameters = logEvent.MessageTemplateParameters; | ||
|
|
||
| if (parameters.Count == 0) | ||
| { | ||
| parameterNames = new HashSet<string>(); | ||
| return ImmutableArray<KeyValuePair<string, object>>.Empty; | ||
| } | ||
|
|
||
| // The HashSet<T> capacity constructor is unavailable on netstandard2.0 and net462 (added in net472). | ||
| #if NETSTANDARD2_0 || NET462 | ||
| parameterNames = new HashSet<string>(); | ||
| #else | ||
| parameterNames = new HashSet<string>(parameters.Count); | ||
| #endif | ||
|
|
||
| var @params = ImmutableArray.CreateBuilder<KeyValuePair<string, object>>(parameters.Count); | ||
|
|
||
| var index = 0; | ||
| foreach (var parameter in parameters) | ||
| { | ||
| // NLog allows passing unnamed holes (e.g. `{}`) - parameters with no names. | ||
| // To prevent a collision on the attribute key when multiple unnamed holes are present, we fall back to the | ||
| // positional index of the parameter in these cases (matching the behaviour of MEL). | ||
| var name = string.IsNullOrEmpty(parameter.Name) | ||
| ? index.ToString(CultureInfo.InvariantCulture) | ||
| : parameter.Name; | ||
|
|
||
| parameterNames.Add(name); | ||
| @params.Add(new KeyValuePair<string, object>(name, parameter.Value)); | ||
| index++; | ||
| } | ||
|
|
||
| return @params.DrainToImmutable(); | ||
| } | ||
| } | ||
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.