Skip to content

Commit

Permalink
fix parameter bug and remove code dupl
Browse files Browse the repository at this point in the history
  • Loading branch information
304NotModified committed Dec 1, 2017
1 parent 3636f98 commit 778f6db
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
30 changes: 18 additions & 12 deletions src/NLog.Extensions.Logging/NLogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private LogEventInfo CreateLogEventInfo(LogLevel nLogLogLevel, string message, I

private static bool IsNonDigitValue(string value)
{
return !string.IsNullOrEmpty(value) && (value.Length != 1 || !char.IsDigit(value[0]));
return !String.IsNullOrEmpty(value) && (value.Length != 1 || !Char.IsDigit(value[0]));
}

#if !NETSTANDARD1_3
Expand Down Expand Up @@ -110,11 +110,7 @@ private LogEventInfo CreateLogEventInfoWithMultipleParameters(LogLevel nLogLogLe
if (string.IsNullOrEmpty(parameter.Key))
break; // Skip capture of invalid parameters

var parameterName = parameter.Key;
if (parameterName[0] == '@' || parameterName[0] == '$')
{
parameterName = parameterName.Substring(1);
}
var parameterName = RemoveMarkerFromName(parameter.Key);
eventInfo.Properties[parameterName] = parameter.Value;
}
return eventInfo;
Expand All @@ -125,11 +121,11 @@ private LogEventInfo CreateLogEventInfoWithMultipleParameters(LogLevel nLogLogLe

private void CaptureEventId(LogEventInfo eventInfo, EventId eventId)
{
if (!_options.IgnoreEmptyEventId || eventId.Id != 0 || !string.IsNullOrEmpty(eventId.Name))
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;
var eventIdSeparator = _options.EventIdSeparator ?? String.Empty;
if (!ReferenceEquals(eventIdPropertyNames.Item1, eventIdSeparator))
{
// Perform atomic cache update of the string-allocations matching the current separator
Expand All @@ -147,8 +143,8 @@ private static Tuple<string, string, string> CreateEventIdPropertyNames(string e
{
var eventIdPropertyNames = new Tuple<string, string, string>(
eventIdSeparator,
string.Concat("EventId", eventIdSeparator, "Id"),
string.Concat("EventId", eventIdSeparator, "Name"));
String.Concat("EventId", eventIdSeparator, "Id"),
String.Concat("EventId", eventIdSeparator, "Name"));
return eventIdPropertyNames;
}

Expand All @@ -158,7 +154,7 @@ private void CaptureMessageProperties<TState>(LogEventInfo eventInfo, TState sta
{
foreach (var property in messageProperties)
{
if (string.IsNullOrEmpty(property.Key))
if (String.IsNullOrEmpty(property.Key))
continue;

eventInfo.Properties[property.Key] = property.Value;
Expand Down Expand Up @@ -234,7 +230,7 @@ public static IDisposable CreateFromState<TState>(TState state, IEnumerable<KeyV
scope.AddDispose(NestedDiagnosticsLogicalContext.Push(state));
return scope;
}

public void AddDispose(IDisposable disposable)
{
Properties.Add(disposable);
Expand Down Expand Up @@ -302,5 +298,15 @@ public IDisposable BeginScope<TState>(TState state)

return NestedDiagnosticsLogicalContext.Push(state);
}

internal static string RemoveMarkerFromName(string parameterName)
{
var firstChar = parameterName[0];
if (firstChar == '@' || firstChar == '$')
{
parameterName = parameterName.Substring(1);
}
return parameterName;
}
}
}
33 changes: 20 additions & 13 deletions src/NLog.Extensions.Logging/NLogMessageParameterList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Threading.Tasks;


namespace NLog.Extensions.Logging
{
#if !NETSTANDARD1_3
Expand Down Expand Up @@ -49,23 +50,29 @@ public NLog.MessageTemplates.MessageTemplateParameter this[int index]
{
var parameter = _parameterList[index];
var parameterName = parameter.Key;
NLog.MessageTemplates.CaptureType captureType = NLog.MessageTemplates.CaptureType.Normal;
switch (parameterName[0])
{
case '@':
parameterName = parameterName.Substring(1);
captureType = NLog.MessageTemplates.CaptureType.Serialize;
break;
case '$':
parameterName = parameterName.Substring(1);
captureType = NLog.MessageTemplates.CaptureType.Stringify;
break;
}
return new NLog.MessageTemplates.MessageTemplateParameter(parameter.Key, parameter.Value, null, captureType);
var capture = GetCaptureType(parameterName);
parameterName = NLogLogger.RemoveMarkerFromName(parameterName);
return new NLog.MessageTemplates.MessageTemplateParameter(parameterName, parameter.Value, null, capture);
}
set => throw new NotSupportedException();
}

private static NLog.MessageTemplates.CaptureType GetCaptureType(string parameterName)
{
var captureType = NLog.MessageTemplates.CaptureType.Normal;

switch (parameterName[0])
{
case '@':
captureType = NLog.MessageTemplates.CaptureType.Serialize;
break;
case '$':
captureType = NLog.MessageTemplates.CaptureType.Stringify;
break;
}
return captureType;
}

public int Count => _parameterList.Count - 1;

public bool IsReadOnly => true;
Expand Down

0 comments on commit 778f6db

Please sign in to comment.