Skip to content

Commit e006c89

Browse files
authored
NLogProviderOptions with support for CaptureEventId (#538)
1 parent 1d8f06a commit e006c89

5 files changed

Lines changed: 50 additions & 23 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
3+
namespace NLog.Extensions.Logging
4+
{
5+
/// <summary>
6+
/// Defines EventId capture options
7+
/// </summary>
8+
[Flags]
9+
public enum EventIdCaptureType
10+
{
11+
/// <summary>
12+
/// Skip capture
13+
/// </summary>
14+
None = 0,
15+
/// <summary>
16+
/// Capture entire <see cref="Microsoft.Extensions.Logging.EventId"/> as "EventId"-property (with boxing)
17+
/// </summary>
18+
EventId = 1,
19+
/// <summary>
20+
/// Capture <see cref="Microsoft.Extensions.Logging.EventId.Id"/> as "EventId_Id"-property.
21+
/// </summary>
22+
EventId_Id = 2,
23+
/// <summary>
24+
/// Capture <see cref="Microsoft.Extensions.Logging.EventId.Name"/> as "EventId_Name"-property.
25+
/// </summary>
26+
EventId_Name = 4,
27+
/// <summary>
28+
/// Capture all properties (Legacy)
29+
/// </summary>
30+
All = EventId | EventId_Id | EventId_Name,
31+
}
32+
}

src/NLog.Extensions.Logging/Logging/NLogLogger.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private static object[] CreateLogEventInfoParameters(NLogMessageParameterList me
200200
}
201201

202202
private static readonly object[] SingleItemArray = { null };
203-
private static readonly IList<MessageTemplateParameter> EmptyParameterArray = new MessageTemplateParameter[] { };
203+
private static readonly IList<MessageTemplateParameter> EmptyParameterArray = Array.Empty<MessageTemplateParameter>();
204204

205205
/// <summary>
206206
/// Are all parameters positional and correctly mapped?
@@ -328,7 +328,7 @@ private static void SetLogEventMessageFormatter(LogEventInfo logEvent, NLogMessa
328328

329329
private void CaptureEventId(LogEventInfo eventInfo, EventId eventId)
330330
{
331-
if (_options.CaptureMessageProperties && (!_options.IgnoreEmptyEventId || eventId.Id != 0 || !String.IsNullOrEmpty(eventId.Name)))
331+
if (_options.CaptureMessageProperties && _options.CaptureEventId != EventIdCaptureType.None && (!_options.IgnoreEmptyEventId || eventId.Id != 0 || !String.IsNullOrEmpty(eventId.Name)))
332332
{
333333
// Attempt to reuse the same string-allocations based on the current <see cref="NLogProviderOptions.EventIdSeparator"/>
334334
var eventIdPropertyNames = _eventIdPropertyNames ?? new Tuple<string, string, string>(null, null, null);
@@ -341,16 +341,12 @@ private void CaptureEventId(LogEventInfo eventInfo, EventId eventId)
341341

342342
var idIsZero = eventId.Id == 0;
343343
var eventIdObj = idIsZero ? ZeroEventId : GetEventId(eventId.Id);
344-
eventInfo.Properties[eventIdPropertyNames.Item2] = eventIdObj;
345-
if (_options.CaptureEntireEventId)
346-
{
344+
if ((_options.CaptureEventId & EventIdCaptureType.EventId_Id) != 0)
345+
eventInfo.Properties[eventIdPropertyNames.Item2] = eventIdObj;
346+
if ((_options.CaptureEventId & EventIdCaptureType.EventId_Name) != 0 && (!string.IsNullOrEmpty(eventId.Name) || !_options.IgnoreEmptyEventId))
347347
eventInfo.Properties[eventIdPropertyNames.Item3] = eventId.Name;
348+
if ((_options.CaptureEventId & EventIdCaptureType.EventId) != 0)
348349
eventInfo.Properties["EventId"] = idIsZero && eventId.Name == null ? EmptyEventId : eventId;
349-
}
350-
else if (!string.IsNullOrEmpty(eventId.Name))
351-
{
352-
eventInfo.Properties[eventIdPropertyNames.Item3] = eventId.Name;
353-
}
354350
}
355351
}
356352

src/NLog.Extensions.Logging/Logging/NLogMessageParameterList.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ internal class NLogMessageParameterList : IList<MessageTemplateParameter>
1414
private static readonly NLogMessageParameterList EmptyList = new NLogMessageParameterList(new KeyValuePair<string, object>[0]);
1515
private static readonly NLogMessageParameterList OriginalMessageList = new NLogMessageParameterList(new[] { new KeyValuePair<string, object>(NLogLogger.OriginalFormatPropertyName, string.Empty) });
1616

17-
public bool HasOriginalMessage => _originalMessageIndex.HasValue;
1817
private readonly int? _originalMessageIndex;
1918

2019
public bool HasComplexParameters => _hasMessageTemplateCapture || _isMixedPositional;
@@ -60,7 +59,7 @@ public static NLogMessageParameterList TryParse(IReadOnlyList<KeyValuePair<strin
6059

6160
public bool HasMessageTemplateSyntax(bool parseMessageTemplates)
6261
{
63-
return HasOriginalMessage && (HasComplexParameters || (parseMessageTemplates && Count > 0));
62+
return _originalMessageIndex.HasValue && (HasComplexParameters || (parseMessageTemplates && Count > 0));
6463
}
6564

6665
public string GetOriginalMessage(IReadOnlyList<KeyValuePair<string, object>> messageProperties)

src/NLog.Extensions.Logging/Logging/NLogProviderOptions.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@ public class NLogProviderOptions
1313
public string EventIdSeparator { get; set; } = "_";
1414

1515
/// <summary>
16-
/// Skip creating "EventId_Id" and "EventId_Name" as <see cref="LogEventInfo.Properties" /> when <c>default(EventId)</c>
16+
/// Skip capture of "EventId_Id" and "EventId_Name" as <see cref="LogEventInfo.Properties" /> when <c>default(EventId)</c>
1717
/// </summary>
1818
public bool IgnoreEmptyEventId { get; set; } = true;
1919

20+
/// <summary>
21+
/// Control capture of <see cref="Microsoft.Extensions.Logging.EventId"/> as "EventId"-property.
22+
/// </summary>
23+
/// <remarks>
24+
/// Enabling capture of the entire "EventId" will increase memory allocation and gives a performance hit. Faster to use "EventId_Id" + "EventId_Name".
25+
/// </remarks>
26+
public EventIdCaptureType CaptureEventId { get; set; } = EventIdCaptureType.EventId_Id | EventIdCaptureType.EventId_Name;
27+
2028
/// <summary>
2129
/// Enable structured logging by capturing message template parameters with support for "@" and "$". Enables use of ${message:raw=true}
2230
/// </summary>
@@ -87,14 +95,6 @@ public class NLogProviderOptions
8795
/// <remarks>Will only attempt to load NLog-LoggingConfiguration if valid section-name, and NLog-LoggingConfiguration has not been loaded already.</remarks>
8896
public string LoggingConfigurationSectionName { get; set; } = "NLog";
8997

90-
/// <summary>
91-
/// Enable additional capture of the entire <see cref="Microsoft.Extensions.Logging.EventId"/> as "EventId"-property.
92-
/// </summary>
93-
/// <remarks>
94-
/// Enabling capture of the entire "EventId" will increase memory allocation and gives a performance hit. Faster to use "EventId_Id" + "EventId_Name".
95-
/// </remarks>
96-
public bool CaptureEntireEventId { get; set; }
97-
9898
/// <summary>
9999
/// Enable NLog Targets and Layouts to perform dependency lookup using the Microsoft Dependency Injection IServiceProvider
100100
/// </summary>

test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void AddNLog_LoggerFactory_LogInfoWithEventId_ShouldLogToNLogWithEventId(
4545
var config = CreateConfigWithMemoryTarget(out var memoryTarget, $"${{event-properties:{eventPropery}}} - ${{message}}");
4646

4747
// Act
48-
loggerFactory.AddNLog(new NLogProviderOptions { EventIdSeparator = "_", CaptureEntireEventId = captureEntireEventId });
48+
loggerFactory.AddNLog(new NLogProviderOptions { EventIdSeparator = "_", CaptureEventId = captureEntireEventId ? EventIdCaptureType.All : EventIdCaptureType.EventId_Id | EventIdCaptureType.EventId_Name });
4949
LogManager.Configuration = config;
5050
var logger = loggerFactory.CreateLogger("logger1");
5151
logger.LogInformation(new EventId(2, "eventId_2"), "test message with {0} arg", 1);
@@ -108,7 +108,7 @@ public void AddNLog_LoggingBuilder_LogInfoWithEventId_ShouldLogToNLogWithEventId
108108
// Arrange
109109
ILoggingBuilder builder = new LoggingBuilderStub();
110110
var config = CreateConfigWithMemoryTarget(out var memoryTarget, $"${{event-properties:{eventPropery}}} - ${{message}}");
111-
var options = new NLogProviderOptions { EventIdSeparator = "_", CaptureEntireEventId = captureEntireEventId };
111+
var options = new NLogProviderOptions { EventIdSeparator = "_", CaptureEventId = captureEntireEventId ? EventIdCaptureType.All : EventIdCaptureType.EventId_Id | EventIdCaptureType.EventId_Name };
112112

113113
// Act
114114
builder.AddNLog(config, options);

0 commit comments

Comments
 (0)