diff --git a/src/NLog.Extensions.Logging/Logging/EventIdCaptureType.cs b/src/NLog.Extensions.Logging/Logging/EventIdCaptureType.cs
index 45242018..67d11849 100644
--- a/src/NLog.Extensions.Logging/Logging/EventIdCaptureType.cs
+++ b/src/NLog.Extensions.Logging/Logging/EventIdCaptureType.cs
@@ -13,20 +13,28 @@ public enum EventIdCaptureType
///
None = 0,
///
- /// Capture entire as "EventId"-property (with boxing)
+ /// Capture entire as "EventId"-property
///
EventId = 1,
///
+ /// Capture entire as "EventName"-property
+ ///
+ EventName = 2,
+ ///
+ /// Capture entire as "EventId"-property (with boxing)
+ ///
+ EventIdStruct = 4,
+ ///
/// Capture as "EventId_Id"-property.
///
- EventId_Id = 2,
+ EventId_Id = 8,
///
/// Capture as "EventId_Name"-property.
///
- EventId_Name = 4,
+ EventId_Name = 16,
///
- /// Capture all properties (Legacy)
+ /// Capture all old properties (Legacy)
///
- All = EventId | EventId_Id | EventId_Name,
+ Legacy = EventIdStruct | EventId_Id | EventId_Name,
}
}
diff --git a/src/NLog.Extensions.Logging/Logging/NLogLogger.cs b/src/NLog.Extensions.Logging/Logging/NLogLogger.cs
index e008e7b4..bc4b9ef6 100644
--- a/src/NLog.Extensions.Logging/Logging/NLogLogger.cs
+++ b/src/NLog.Extensions.Logging/Logging/NLogLogger.cs
@@ -299,23 +299,32 @@ private void CaptureEventIdProperties(LogEventInfo logEvent, EventId eventId)
var captureEventId = _options.CaptureEventId;
if (captureEventId != EventIdCaptureType.None && IncludeEventIdProperties(eventId))
{
- // Attempt to reuse the same string-allocations based on the current
- var eventIdPropertyNames = _eventIdPropertyNames ?? new Tuple(null, null, null);
- var eventIdSeparator = _options.EventIdSeparator ?? String.Empty;
- if (!ReferenceEquals(eventIdPropertyNames.Item1, eventIdSeparator))
+ if ((captureEventId & EventIdCaptureType.EventId) != 0)
+ logEvent.Properties[nameof(EventId)] = eventId.Id == 0 ? ZeroEventId : GetEventId(eventId.Id);
+
+ if ((captureEventId & EventIdCaptureType.EventName) != 0 && eventId.Name != null)
+ logEvent.Properties["EventName"] = eventId.Name;
+
+ if ((captureEventId & EventIdCaptureType.Legacy) != 0)
{
- // Perform atomic cache update of the string-allocations matching the current separator
- _eventIdPropertyNames = eventIdPropertyNames = CreateEventIdPropertyNames(eventIdSeparator);
- }
+ // Attempt to reuse the same string-allocations based on the current
+ var eventIdPropertyNames = _eventIdPropertyNames ?? new Tuple(null, null, null);
+ var eventIdSeparator = _options.EventIdSeparator ?? String.Empty;
+ if (!ReferenceEquals(eventIdPropertyNames.Item1, eventIdSeparator))
+ {
+ // Perform atomic cache update of the string-allocations matching the current separator
+ _eventIdPropertyNames = eventIdPropertyNames = CreateEventIdPropertyNames(eventIdSeparator);
+ }
- if ((captureEventId & EventIdCaptureType.EventId_Id) != 0)
- logEvent.Properties[eventIdPropertyNames.Item2] = eventId.Id == 0 ? ZeroEventId : GetEventId(eventId.Id);
+ if ((captureEventId & EventIdCaptureType.EventId_Id) != 0)
+ logEvent.Properties[eventIdPropertyNames.Item2] = eventId.Id == 0 ? ZeroEventId : GetEventId(eventId.Id);
- if ((captureEventId & EventIdCaptureType.EventId_Name) != 0 && eventId.Name != null)
- logEvent.Properties[eventIdPropertyNames.Item3] = eventId.Name;
+ 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;
+ if ((captureEventId & EventIdCaptureType.EventIdStruct) != 0)
+ logEvent.Properties[nameof(EventId)] = eventId;
+ }
}
}
diff --git a/src/NLog.Extensions.Logging/Logging/NLogProviderOptions.cs b/src/NLog.Extensions.Logging/Logging/NLogProviderOptions.cs
index 749d45bd..545fc533 100644
--- a/src/NLog.Extensions.Logging/Logging/NLogProviderOptions.cs
+++ b/src/NLog.Extensions.Logging/Logging/NLogProviderOptions.cs
@@ -10,10 +10,13 @@ public class NLogProviderOptions
///
/// Separator between for EventId.Id and EventId.Name. Default to _
///
+ ///
+ /// Only relevant for , or
+ ///
public string EventIdSeparator { get; set; } = "_";
///
- /// Skip capture of "EventId_Id" and "EventId_Name" as when default(EventId)
+ /// Skip skip capture of in when default(EventId)
///
public bool IgnoreEmptyEventId { get; set; } = true;
@@ -21,9 +24,9 @@ public class NLogProviderOptions
/// 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".
+ /// Enabling capture of the entire "EventId" will increase memory allocation and gives a performance hit. Faster to use "EventId" + "EventName".
///
- public EventIdCaptureType CaptureEventId { get; set; } = EventIdCaptureType.EventId_Id | EventIdCaptureType.EventId_Name;
+ public EventIdCaptureType CaptureEventId { get; set; } = EventIdCaptureType.EventId | EventIdCaptureType.EventName;
///
/// Enable structured logging by capturing message template parameters with support for "@" and "$". Enables use of ${message:raw=true}
diff --git a/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs b/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs
index f932ce7a..3d99d6b7 100644
--- a/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs
+++ b/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs
@@ -32,11 +32,13 @@ public void AddNLog_LoggerFactory_LogInfo_ShouldLogToNLog()
[Theory]
[InlineData("EventId", "eventId_2", true)]
- [InlineData("EventId_Name", "eventId_2", true)]
+ [InlineData("EventName", "", true)]
[InlineData("EventId_Id", "2", true)]
- [InlineData("EventId", "", false)]
- [InlineData("EventId_Name", "eventId_2", false)]
- [InlineData("EventId_Id", "2", false)]
+ [InlineData("EventId_Name", "eventId_2", true)]
+ [InlineData("EventId", "2", false)]
+ [InlineData("EventName", "eventId_2", false)]
+ [InlineData("EventId_Id", "", false)]
+ [InlineData("EventId_Name", "", false)]
[Obsolete("Instead use ILoggingBuilder.AddNLog() or IHostBuilder.UseNLog()")]
public void AddNLog_LoggerFactory_LogInfoWithEventId_ShouldLogToNLogWithEventId(string eventPropery, string expectedEventInLog, bool captureEntireEventId)
{
@@ -45,7 +47,7 @@ public void AddNLog_LoggerFactory_LogInfoWithEventId_ShouldLogToNLogWithEventId(
var config = CreateConfigWithMemoryTarget(out var memoryTarget, $"${{event-properties:{eventPropery}}} - ${{message}}");
// Act
- loggerFactory.AddNLog(new NLogProviderOptions { EventIdSeparator = "_", CaptureEventId = captureEntireEventId ? EventIdCaptureType.All : EventIdCaptureType.EventId_Id | EventIdCaptureType.EventId_Name });
+ loggerFactory.AddNLog(new NLogProviderOptions { EventIdSeparator = "_", CaptureEventId = captureEntireEventId ? EventIdCaptureType.Legacy : (EventIdCaptureType.EventId | EventIdCaptureType.EventName) });
LogManager.Configuration = config;
var logger = loggerFactory.CreateLogger("logger1");
logger.LogInformation(new EventId(2, "eventId_2"), "test message with {0} arg", 1);
@@ -98,17 +100,19 @@ public void AddNLog_LoggingBuilder_LogInfo_ShouldLogToNLog()
[Theory]
[InlineData("EventId", "eventId2", true)]
+ [InlineData("EventName", "", true)]
[InlineData("EventId_Name", "eventId2", true)]
[InlineData("EventId_Id", "2", true)]
- [InlineData("EventId", "", false)]
- [InlineData("EventId_Name", "eventId2", false)]
- [InlineData("EventId_Id", "2", false)]
+ [InlineData("EventId", "2", false)]
+ [InlineData("EventName", "eventId2", false)]
+ [InlineData("EventId_Id", "", false)]
+ [InlineData("EventId_Name", "", false)]
public void AddNLog_LoggingBuilder_LogInfoWithEventId_ShouldLogToNLogWithEventId(string eventPropery, string expectedEventInLog, bool captureEntireEventId)
{
// Arrange
ILoggingBuilder builder = new LoggingBuilderStub();
var config = CreateConfigWithMemoryTarget(out var memoryTarget, $"${{event-properties:{eventPropery}}} - ${{message}}");
- var options = new NLogProviderOptions { EventIdSeparator = "_", CaptureEventId = captureEntireEventId ? EventIdCaptureType.All : EventIdCaptureType.EventId_Id | EventIdCaptureType.EventId_Name };
+ var options = new NLogProviderOptions { EventIdSeparator = "_", CaptureEventId = captureEntireEventId ? EventIdCaptureType.Legacy : (EventIdCaptureType.EventId | EventIdCaptureType.EventName) };
// Act
builder.AddNLog(config, options);
diff --git a/test/NLog.Extensions.Logging.Tests/nlog.config b/test/NLog.Extensions.Logging.Tests/nlog.config
index deb5d7b4..87af5163 100644
--- a/test/NLog.Extensions.Logging.Tests/nlog.config
+++ b/test/NLog.Extensions.Logging.Tests/nlog.config
@@ -1,20 +1,16 @@
-
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-
+ layout="${logger}|${uppercase:${level}}|${message} ${exception}|${event-properties:EventId}${event-properties:ParameterCount}${scopeproperty:item=scope1}" />
-
\ No newline at end of file