-
-
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
Changes from 9 commits
96f2581
de54732
f558baf
07330e7
ac37cef
7e572c9
e2f8cc4
5f3c30c
4a228d0
4716b5c
70e19bc
35afe6f
2da7f13
2b8c331
44a9552
9775861
842e133
b0fea90
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,86 @@ | ||
| namespace Sentry.NLog; | ||
|
|
||
| public sealed partial class SentryTarget | ||
| { | ||
| private static void CaptureStructuredLog(IHub hub, SentryOptions options, LogEventInfo logEvent) | ||
| { | ||
| var level = logEvent.Level.ToSentryLogLevel(); | ||
| if (level.HasValue) | ||
| { | ||
| DateTimeOffset timestamp = new(logEvent.TimeStamp); | ||
| GetStructuredLoggingParametersAndAttributes(logEvent, out var parameters, out var attributes); | ||
|
|
||
| var log = SentryLog.Create(hub, timestamp, level.Value, logEvent.FormattedMessage, logEvent.Message, parameters); | ||
|
|
||
| log.SetDefaultAttributes(options, Sdk); | ||
|
Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs
|
||
| 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); | ||
|
cursor[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| 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 = new List<KeyValuePair<string, object>>(); | ||
|
|
||
| if (logEvent.HasProperties) | ||
| { | ||
| 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) | ||
| { | ||
| // Unnamed holes (e.g. `{}`) have an empty name. Fall back to the positional index so that | ||
| // multiple unnamed holes don't collide on the same `sentry.message.parameter.` attribute key. | ||
| 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++; | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| return @params.DrainToImmutable(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.