diff --git a/src/NLog.Extensions.Logging/Logging/EventIdCaptureType.cs b/src/NLog.Extensions.Logging/Logging/EventIdCaptureType.cs new file mode 100644 index 00000000..45242018 --- /dev/null +++ b/src/NLog.Extensions.Logging/Logging/EventIdCaptureType.cs @@ -0,0 +1,32 @@ +using System; + +namespace NLog.Extensions.Logging +{ + /// + /// Defines EventId capture options + /// + [Flags] + public enum EventIdCaptureType + { + /// + /// Skip capture + /// + None = 0, + /// + /// Capture entire as "EventId"-property (with boxing) + /// + EventId = 1, + /// + /// Capture as "EventId_Id"-property. + /// + EventId_Id = 2, + /// + /// Capture as "EventId_Name"-property. + /// + EventId_Name = 4, + /// + /// Capture all properties (Legacy) + /// + All = EventId | EventId_Id | EventId_Name, + } +} diff --git a/src/NLog.Extensions.Logging/Logging/NLogLogger.cs b/src/NLog.Extensions.Logging/Logging/NLogLogger.cs index 8039ac32..68c33563 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogLogger.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogLogger.cs @@ -200,7 +200,7 @@ private static object[] CreateLogEventInfoParameters(NLogMessageParameterList me } private static readonly object[] SingleItemArray = { null }; - private static readonly IList EmptyParameterArray = new MessageTemplateParameter[] { }; + private static readonly IList EmptyParameterArray = Array.Empty(); /// /// Are all parameters positional and correctly mapped? @@ -328,7 +328,7 @@ private static void SetLogEventMessageFormatter(LogEventInfo logEvent, NLogMessa private void CaptureEventId(LogEventInfo eventInfo, EventId eventId) { - if (_options.CaptureMessageProperties && (!_options.IgnoreEmptyEventId || eventId.Id != 0 || !String.IsNullOrEmpty(eventId.Name))) + if (_options.CaptureMessageProperties && _options.CaptureEventId != EventIdCaptureType.None && (!_options.IgnoreEmptyEventId || eventId.Id != 0 || !String.IsNullOrEmpty(eventId.Name))) { // Attempt to reuse the same string-allocations based on the current var eventIdPropertyNames = _eventIdPropertyNames ?? new Tuple(null, null, null); @@ -341,16 +341,12 @@ private void CaptureEventId(LogEventInfo eventInfo, EventId eventId) var idIsZero = eventId.Id == 0; var eventIdObj = idIsZero ? ZeroEventId : GetEventId(eventId.Id); - eventInfo.Properties[eventIdPropertyNames.Item2] = eventIdObj; - if (_options.CaptureEntireEventId) - { + if ((_options.CaptureEventId & EventIdCaptureType.EventId_Id) != 0 && (!idIsZero || !_options.IgnoreEmptyEventId)) + eventInfo.Properties[eventIdPropertyNames.Item2] = eventIdObj; + if ((_options.CaptureEventId & EventIdCaptureType.EventId_Name) != 0 && (!string.IsNullOrEmpty(eventId.Name) || !_options.IgnoreEmptyEventId)) eventInfo.Properties[eventIdPropertyNames.Item3] = eventId.Name; + if ((_options.CaptureEventId & EventIdCaptureType.EventId) != 0) eventInfo.Properties["EventId"] = idIsZero && eventId.Name == null ? EmptyEventId : eventId; - } - else if (!string.IsNullOrEmpty(eventId.Name)) - { - eventInfo.Properties[eventIdPropertyNames.Item3] = eventId.Name; - } } } diff --git a/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs b/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs index 004aee31..f9c440ac 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs @@ -14,7 +14,6 @@ internal class NLogMessageParameterList : IList private static readonly NLogMessageParameterList EmptyList = new NLogMessageParameterList(new KeyValuePair[0]); private static readonly NLogMessageParameterList OriginalMessageList = new NLogMessageParameterList(new[] { new KeyValuePair(NLogLogger.OriginalFormatPropertyName, string.Empty) }); - public bool HasOriginalMessage => _originalMessageIndex.HasValue; private readonly int? _originalMessageIndex; public bool HasComplexParameters => _hasMessageTemplateCapture || _isMixedPositional; @@ -60,7 +59,7 @@ public static NLogMessageParameterList TryParse(IReadOnlyList 0)); + return _originalMessageIndex.HasValue && (HasComplexParameters || (parseMessageTemplates && Count > 0)); } public string GetOriginalMessage(IReadOnlyList> messageProperties) diff --git a/src/NLog.Extensions.Logging/Logging/NLogProviderOptions.cs b/src/NLog.Extensions.Logging/Logging/NLogProviderOptions.cs index 177bfe5c..749d45bd 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogProviderOptions.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogProviderOptions.cs @@ -13,10 +13,18 @@ public class NLogProviderOptions public string EventIdSeparator { get; set; } = "_"; /// - /// Skip creating "EventId_Id" and "EventId_Name" as when default(EventId) + /// Skip capture of "EventId_Id" and "EventId_Name" as when default(EventId) /// public bool IgnoreEmptyEventId { get; set; } = true; + /// + /// Control capture of as "EventId"-property. + /// + /// + /// Enabling capture of the entire "EventId" will increase memory allocation and gives a performance hit. Faster to use "EventId_Id" + "EventId_Name". + /// + public EventIdCaptureType CaptureEventId { get; set; } = EventIdCaptureType.EventId_Id | EventIdCaptureType.EventId_Name; + /// /// Enable structured logging by capturing message template parameters with support for "@" and "$". Enables use of ${message:raw=true} /// @@ -87,14 +95,6 @@ public class NLogProviderOptions /// Will only attempt to load NLog-LoggingConfiguration if valid section-name, and NLog-LoggingConfiguration has not been loaded already. public string LoggingConfigurationSectionName { get; set; } = "NLog"; - /// - /// Enable additional capture of the entire as "EventId"-property. - /// - /// - /// Enabling capture of the entire "EventId" will increase memory allocation and gives a performance hit. Faster to use "EventId_Id" + "EventId_Name". - /// - public bool CaptureEntireEventId { get; set; } - /// /// Enable NLog Targets and Layouts to perform dependency lookup using the Microsoft Dependency Injection IServiceProvider ///