-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NLogMessageParameterList - Reduce code complexity for IsValidParameterList #333
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ namespace NLog.Extensions.Logging | |
internal class NLogMessageParameterList : IList<MessageTemplateParameter> | ||
{ | ||
private readonly IReadOnlyList<KeyValuePair<string, object>> _parameterList; | ||
private static readonly NLogMessageParameterList EmptyList = new NLogMessageParameterList(new KeyValuePair<string, object>[0]); | ||
private static readonly NLogMessageParameterList EmptyList = new NLogMessageParameterList(new KeyValuePair<string, object>[0]); | ||
private static readonly NLogMessageParameterList OriginalMessageList = new NLogMessageParameterList(new[] { new KeyValuePair<string, object>(NLogLogger.OriginalFormatPropertyName, string.Empty) }); | ||
|
||
public bool HasOriginalMessage => _originalMessageIndex.HasValue; | ||
|
@@ -44,7 +44,7 @@ public NLogMessageParameterList(IReadOnlyList<KeyValuePair<string, object>> para | |
/// </remarks> | ||
public static NLogMessageParameterList TryParse(IReadOnlyList<KeyValuePair<string, object>> parameterList) | ||
{ | ||
if (parameterList.Count > 1 || (parameterList.Count == 1 && parameterList[0].Key != NLogLogger.OriginalFormatPropertyName)) | ||
if (parameterList.Count > 1 || (parameterList.Count == 1 && !NLogLogger.OriginalFormatPropertyName.Equals(parameterList[0].Key))) | ||
{ | ||
return new NLogMessageParameterList(parameterList); | ||
} | ||
|
@@ -79,47 +79,43 @@ private static bool IsValidParameterList(IReadOnlyList<KeyValuePair<string, obje | |
{ | ||
hasMessageTemplateCapture = false; | ||
isMixedPositional = false; | ||
isPositional = false; | ||
originalMessageIndex = null; | ||
bool? firstParameterIsPositional = null; | ||
isPositional = false; | ||
string parameterName; | ||
|
||
for (int i = 0; i < parameterList.Count; ++i) | ||
{ | ||
if (!TryGetKey(parameterList, ref originalMessageIndex, i, out var parameterKey)) | ||
if (!TryGetParameterName(parameterList, i, ref originalMessageIndex, out parameterName)) | ||
{ | ||
return false; | ||
} | ||
|
||
char firstChar = parameterKey[0]; | ||
if (GetCaptureType(firstChar) != CaptureType.Normal) | ||
char firstChar = parameterName[0]; | ||
if (firstChar >= '0' && firstChar <= '9') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this check is more expensive than before. but maybe not really significant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most users will always use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The goal of this PR is to reduce the complexity of |
||
{ | ||
hasMessageTemplateCapture = true; | ||
if (!isPositional && i != 0) | ||
isMixedPositional = true; | ||
isPositional = true; | ||
} | ||
else if (parameterKey == NLogLogger.OriginalFormatPropertyName) | ||
else | ||
{ | ||
if (originalMessageIndex.HasValue) | ||
if (isPositional) | ||
{ | ||
originalMessageIndex = null; | ||
return false; | ||
isMixedPositional = true; | ||
} | ||
|
||
originalMessageIndex = i; | ||
} | ||
else | ||
{ | ||
if (!firstParameterIsPositional.HasValue) | ||
firstParameterIsPositional = char.IsDigit(firstChar); | ||
else if (char.IsDigit(firstChar) != firstParameterIsPositional) | ||
isMixedPositional = true; | ||
if (GetCaptureType(firstChar) != CaptureType.Normal) | ||
{ | ||
hasMessageTemplateCapture = true; | ||
} | ||
} | ||
} | ||
|
||
if (firstParameterIsPositional == true && !isMixedPositional) | ||
isPositional = true; | ||
|
||
isPositional = isPositional && !isMixedPositional; | ||
return true; | ||
} | ||
|
||
private static bool TryGetKey(IReadOnlyList<KeyValuePair<string, object>> parameterList, ref int? originalMessageIndex, int i, out string parameterKey) | ||
private static bool TryGetParameterName(IReadOnlyList<KeyValuePair<string, object>> parameterList, int i, ref int? originalMessageIndex, out string parameterKey) | ||
{ | ||
try | ||
{ | ||
|
@@ -137,6 +133,17 @@ private static bool TryGetKey(IReadOnlyList<KeyValuePair<string, object>> parame | |
return false; | ||
} | ||
|
||
if (NLogLogger.OriginalFormatPropertyName.Equals(parameterKey)) | ||
{ | ||
if (originalMessageIndex.HasValue) | ||
{ | ||
originalMessageIndex = null; | ||
return false; | ||
} | ||
|
||
originalMessageIndex = i; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
@@ -152,7 +159,7 @@ private static IReadOnlyList<KeyValuePair<string, object>> CreateValidParameterL | |
if (string.IsNullOrEmpty(paramPair.Key)) | ||
continue; | ||
|
||
if (paramPair.Key == NLogLogger.OriginalFormatPropertyName) | ||
if (NLogLogger.OriginalFormatPropertyName.Equals(paramPair.Key)) | ||
{ | ||
continue; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this one moved to the outer scope?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Old c++ habbit. No need to push the same parameter on the stack for every loop-iteration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my experience is could cause memory leaks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I think it's a code smell in Sonar, not sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only tried memory leaks in C# with dangling event-handlers or dispose-handles (keeps things alive after lifetime) or dynamic-compiled-code (can never be freed).