Skip to content
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

NLogLogger refactoring for faster EventId-property capture #541

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/NLog.Extensions.Logging/Logging/NLogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ private static void SetLogEventMessageFormatter(LogEventInfo logEvent, NLogMessa

private void CaptureEventIdProperties(LogEventInfo logEvent, EventId eventId)
{
if (IncludeEventIdProperties(eventId))
var captureEventId = _options.CaptureEventId;
if (captureEventId != EventIdCaptureType.None && IncludeEventIdProperties(eventId))
{
// Attempt to reuse the same string-allocations based on the current <see cref="NLogProviderOptions.EventIdSeparator"/>
var eventIdPropertyNames = _eventIdPropertyNames ?? new Tuple<string, string, string>(null, null, null);
Expand All @@ -307,20 +308,20 @@ private void CaptureEventIdProperties(LogEventInfo logEvent, EventId eventId)
_eventIdPropertyNames = eventIdPropertyNames = CreateEventIdPropertyNames(eventIdSeparator);
}

if ((_options.CaptureEventId & EventIdCaptureType.EventId_Name) != 0 && eventId.Name != null)
logEvent.Properties[eventIdPropertyNames.Item3] = eventId.Name;

if ((_options.CaptureEventId & EventIdCaptureType.EventId_Id) != 0 && (eventId.Id != 0 || eventId.Name != null))
if ((captureEventId & EventIdCaptureType.EventId_Id) != 0)
logEvent.Properties[eventIdPropertyNames.Item2] = eventId.Id == 0 ? ZeroEventId : GetEventId(eventId.Id);

if ((_options.CaptureEventId & EventIdCaptureType.EventId) != 0)
if ((captureEventId & EventIdCaptureType.EventId_Name) != 0 && eventId.Name != null)
logEvent.Properties[eventIdPropertyNames.Item3] = eventId.Name;

if ((captureEventId & EventIdCaptureType.EventId) != 0)
logEvent.Properties[nameof(EventId)] = eventId;
}
}

private bool IncludeEventIdProperties(EventId eventId)
{
return (eventId.Id != 0 || !string.IsNullOrEmpty(eventId.Name) || !_options.IgnoreEmptyEventId) && _options.CaptureEventId != EventIdCaptureType.None;
return (eventId.Id != 0 || !string.IsNullOrEmpty(eventId.Name) || !_options.IgnoreEmptyEventId);
}

private static Tuple<string, string, string> CreateEventIdPropertyNames(string eventIdSeparator)
Expand Down