Skip to content

Commit

Permalink
Merge pull request #137 from NLog/cleanup
Browse files Browse the repository at this point in the history
Cleanup, update docs
  • Loading branch information
304NotModified authored Sep 29, 2017
2 parents c0ebed4 + 69f879e commit 171ed92
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
66 changes: 33 additions & 33 deletions src/NLog.Extensions.Logging/NLogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ internal class NLogLogger : Microsoft.Extensions.Logging.ILogger
private readonly Logger _logger;
private readonly NLogProviderOptions _options;

private static readonly object _emptyEventId = default(EventId); // Cache boxing of empty EventId-struct
private static readonly object _zeroEventId = default(EventId).Id; // Cache boxing of zero EventId-Value
private static readonly object EmptyEventId = default(EventId); // Cache boxing of empty EventId-struct
private static readonly object ZeroEventId = default(EventId).Id; // Cache boxing of zero EventId-Value
private Tuple<string, string, string> _eventIdPropertyNames;

public NLogLogger(Logger logger, NLogProviderOptions options)
Expand All @@ -25,39 +25,40 @@ public NLogLogger(Logger logger, NLogProviderOptions options)
public void Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
var nLogLogLevel = ConvertLogLevel(logLevel);
if (IsEnabled(nLogLogLevel))
if (!IsEnabled(nLogLogLevel))
{
if (formatter == null)
{
throw new ArgumentNullException(nameof(formatter));
}
var message = formatter(state, exception);
return;
}
if (formatter == null)
{
throw new ArgumentNullException(nameof(formatter));
}
var message = formatter(state, exception);

//message arguments are not needed as it is already checked that the loglevel is enabled.
var eventInfo = LogEventInfo.Create(nLogLogLevel, _logger.Name, message);
eventInfo.Exception = exception;
if (!_options.IgnoreEmptyEventId || eventId.Id != 0 || !string.IsNullOrEmpty(eventId.Name))
//message arguments are not needed as it is already checked that the loglevel is enabled.
var eventInfo = LogEventInfo.Create(nLogLogLevel, _logger.Name, message);
eventInfo.Exception = exception;
if (!_options.IgnoreEmptyEventId || eventId.Id != 0 || !string.IsNullOrEmpty(eventId.Name))
{
// 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);
var eventIdSeparator = _options.EventIdSeparator ?? string.Empty;
if (!ReferenceEquals(eventIdPropertyNames.Item1, eventIdSeparator))
{
// 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);
var eventIdSeparator = _options.EventIdSeparator ?? string.Empty;
if (!ReferenceEquals(eventIdPropertyNames.Item1, eventIdSeparator))
{
// Perform atomic cache update of the string-allocations matching the current separator
eventIdPropertyNames = new Tuple<string, string, string>(
eventIdSeparator,
string.Concat("EventId", eventIdSeparator, "Id"),
string.Concat("EventId", eventIdSeparator, "Name"));
_eventIdPropertyNames = eventIdPropertyNames;
}

eventInfo.Properties[eventIdPropertyNames.Item2] = eventId.Id == 0 ? _zeroEventId : eventId.Id;
eventInfo.Properties[eventIdPropertyNames.Item3] = eventId.Name;
eventInfo.Properties["EventId"] = (eventId.Id == 0 && eventId.Name == null) ? _emptyEventId : eventId;
// Perform atomic cache update of the string-allocations matching the current separator
eventIdPropertyNames = new Tuple<string, string, string>(
eventIdSeparator,
string.Concat("EventId", eventIdSeparator, "Id"),
string.Concat("EventId", eventIdSeparator, "Name"));
_eventIdPropertyNames = eventIdPropertyNames;
}
_logger.Log(eventInfo);


var idIsZero = eventId.Id == 0;
eventInfo.Properties[eventIdPropertyNames.Item2] = idIsZero ? ZeroEventId : eventId.Id;
eventInfo.Properties[eventIdPropertyNames.Item3] = eventId.Name;
eventInfo.Properties["EventId"] = idIsZero && eventId.Name == null ? EmptyEventId : eventId;
}
_logger.Log(eventInfo);
}

/// <summary>
Expand Down Expand Up @@ -108,10 +109,9 @@ private static LogLevel ConvertLogLevel(Microsoft.Extensions.Logging.LogLevel lo
}

/// <summary>
/// Begin a scope. Log in config with ${ndc}
/// TODO not working with async
/// Begin a scope. Use in config with ${ndlc}
/// </summary>
/// <param name="state">The state</param>
/// <param name="state">The state (message)</param>
/// <returns></returns>
public IDisposable BeginScope<TState>(TState state)
{
Expand Down
6 changes: 4 additions & 2 deletions src/NLog.Extensions.Logging/NLogProviderOptions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NLog.Extensions.Logging
{
Expand All @@ -13,8 +12,11 @@ public class NLogProviderOptions
public string EventIdSeparator { get; set; }

/// <summary>
/// Skip allocation of <see cref="LogEventInfo.Properties" />-dictionary when <see cref="default(EventId)"/>
/// Skip allocation of <see cref="LogEventInfo.Properties" />-dictionary
/// </summary>
/// <remarks>
/// using
/// <c>default(EventId)</c></remarks>
public bool IgnoreEmptyEventId { get; set; }

/// <summary>Initializes a new instance of the <see cref="T:System.Object" /> class.</summary>
Expand Down

0 comments on commit 171ed92

Please sign in to comment.