Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/NLogPublic

Commit92ffecb

Browse files
committed
Reduce code complexity
1 parent54f7c93 commit92ffecb

File tree

3 files changed

+21
-104
lines changed

3 files changed

+21
-104
lines changed

‎src/NLog/Internal/PropertiesDictionary.cs‎

Lines changed: 7 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace NLog.Internal
4747
/// in the collection, and in positional order.
4848
/// </summary>
4949
[DebuggerDisplay("Count = {Count}")]
50-
internalsealedclassPropertiesDictionary:IDictionary<object,object?>,IList<MessageTemplateParameter>
50+
internalsealedclassPropertiesDictionary:IDictionary<object,object?>
5151
{
5252
private
5353
#if!NETFRAMEWORK
@@ -124,7 +124,7 @@ private Dictionary<object, PropertyValue> EventProperties
124124
}
125125
}
126126

127-
publicIList<MessageTemplateParameter>MessageProperties=>this;
127+
publicMessageTemplateParameter[]MessageProperties=>_messagePropertiesCount>0&&_messageProperties?.Length==_messagePropertiesCount?_messageProperties:ArrayHelper.Empty<MessageTemplateParameter>();
128128

129129
publicvoidResetMessageProperties(MessageTemplateParameter[]?newMessageProperties=null,intnewMessagePropertiesCount=0)
130130
{
@@ -198,26 +198,6 @@ private static Dictionary<object, PropertyValue> BuildEventProperties(MessageTem
198198
/// <inheritDoc/>
199199
publicboolIsReadOnly=>false;
200200

201-
intICollection<MessageTemplateParameter>.Count=>_messagePropertiesCount;
202-
203-
boolICollection<MessageTemplateParameter>.IsReadOnly=>true;
204-
205-
MessageTemplateParameterIList<MessageTemplateParameter>.this[intindex]
206-
{
207-
get
208-
{
209-
if(index>=_messagePropertiesCount||index<0||_messagePropertiesisnull)
210-
thrownewArgumentOutOfRangeException(nameof(index));
211-
return_messageProperties[index];
212-
}
213-
set
214-
{
215-
if(index>=_messagePropertiesCount||index<0||_messagePropertiesisnull)
216-
thrownewArgumentOutOfRangeException(nameof(index));
217-
_messageProperties[index]=value;
218-
}
219-
}
220-
221201
/// <inheritDoc/>
222202
publicobject?this[objectkey]
223203
{
@@ -271,11 +251,6 @@ public void Add(object key, object? value)
271251
EventProperties.Add(key,newPropertyValue(value,false));
272252
}
273253

274-
privateboolSkipDictionaryAllocation()
275-
{
276-
return_eventPropertiesisnull&&(_messagePropertiesisnull||_messagePropertiesCount<_messageProperties.Length);
277-
}
278-
279254
/// <inheritDoc/>
280255
publicvoidAdd(KeyValuePair<object,object?>item)
281256
{
@@ -432,6 +407,11 @@ private bool TryLookupMessagePropertyValue(object key, out object? propertyValue
432407
returnfalse;
433408
}
434409

410+
privateboolSkipDictionaryAllocation()
411+
{
412+
return_eventPropertiesisnull&&(_messagePropertiesisnull||_messagePropertiesCount<_messageProperties.Length);
413+
}
414+
435415
/// <summary>
436416
/// Check if the message-template-parameters can be used directly without allocating a dictionary
437417
/// </summary>
@@ -514,65 +494,6 @@ internal static string GenerateUniquePropertyName<TKey, TValue>(string originalN
514494
returnnewItemName;
515495
}
516496

517-
intIList<MessageTemplateParameter>.IndexOf(MessageTemplateParameteritem)
518-
{
519-
if(_messageProperties!=null&&_messagePropertiesCount>0)
520-
{
521-
for(inti=0;i<_messagePropertiesCount;++i)
522-
{
523-
if(_messageProperties[i].Equals(item))
524-
returni;
525-
}
526-
}
527-
return-1;
528-
}
529-
530-
boolICollection<MessageTemplateParameter>.Contains(MessageTemplateParameteritem)
531-
{
532-
return((IList<MessageTemplateParameter>)this).IndexOf(item)>=0;
533-
}
534-
535-
voidICollection<MessageTemplateParameter>.CopyTo(MessageTemplateParameter[]array,intarrayIndex)
536-
{
537-
if(_messageProperties!=null&&_messagePropertiesCount>0)
538-
{
539-
Array.Copy(_messageProperties,0,array,arrayIndex,_messagePropertiesCount);
540-
}
541-
}
542-
543-
voidIList<MessageTemplateParameter>.Insert(intindex,MessageTemplateParameteritem)
544-
{
545-
thrownewNotSupportedException("MessageTemplateParameters array is read-only");
546-
}
547-
548-
voidIList<MessageTemplateParameter>.RemoveAt(intindex)
549-
{
550-
thrownewNotSupportedException("MessageTemplateParameters array is read-only");
551-
}
552-
553-
voidICollection<MessageTemplateParameter>.Add(MessageTemplateParameteritem)
554-
{
555-
thrownewNotSupportedException("MessageTemplateParameters array is read-only");
556-
}
557-
558-
voidICollection<MessageTemplateParameter>.Clear()
559-
{
560-
thrownewNotSupportedException("MessageTemplateParameters array is read-only");
561-
}
562-
563-
boolICollection<MessageTemplateParameter>.Remove(MessageTemplateParameteritem)
564-
{
565-
thrownewNotSupportedException("MessageTemplateParameters array is read-only");
566-
}
567-
568-
IEnumerator<MessageTemplateParameter>IEnumerable<MessageTemplateParameter>.GetEnumerator()
569-
{
570-
if(_messagePropertiesisnull)
571-
returnSystem.Linq.Enumerable.Empty<MessageTemplateParameter>().GetEnumerator();
572-
else
573-
return((IList<MessageTemplateParameter>)_messageProperties).GetEnumerator();
574-
}
575-
576497
publicstructPropertyDictionaryEnumerator:IEnumerator<KeyValuePair<object,object?>>
577498
{
578499
privatereadonlyPropertiesDictionary_dictionary;

‎src/NLog/LogEventInfo.cs‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ public MessageTemplateParameters MessageTemplateParameters
465465
{
466466
get
467467
{
468-
if(_properties?.MessageProperties.Count>0)
468+
if(_properties?.MessageProperties.Length>0)
469469
{
470470
returnnewMessageTemplateParameters(_properties.MessageProperties);
471471
}
@@ -711,21 +711,20 @@ internal bool IsLogEventThreadAgnosticImmutable()
711711
if(properties.Count>5)
712712
returnfalse;// too many properties, too costly to check
713713

714-
if(properties.Count==_parameters?.Length&&properties.Count==properties.MessageProperties.Count)
714+
if(properties.Count==_parameters?.Length&&properties.Count==properties.MessageProperties.Length)
715715
returntrue;// Already checked formatted message, no need to do it twice
716716

717717
returnHasImmutableProperties(properties);
718718
}
719719

720720
privatestaticboolHasImmutableProperties(PropertiesDictionaryproperties)
721721
{
722-
varmessagePropertiesCount=properties.MessageProperties.Count;
723-
if(properties.Count==messagePropertiesCount)
722+
varmessageProperties=properties.MessageProperties;
723+
if(properties.Count==messageProperties.Length)
724724
{
725725
// Skip enumerator allocation when all properties comes from the message-template
726-
for(inti=0;i<messagePropertiesCount;++i)
726+
foreach(varpropertyinmessageProperties)
727727
{
728-
varproperty=properties.MessageProperties[i];
729728
if(!IsSafeToDeferFormatting(property.Value))
730729
returnfalse;
731730
}
@@ -841,7 +840,7 @@ private bool ResetMessageTemplateParameters()
841840
}
842841

843842
// If message-template-properties have not been provided as contructor-input, then allow parsing of message-template.
844-
return_properties.MessageProperties.Count==0||_properties.MessageProperties[0].CaptureType==CaptureType.Unknown;
843+
return_properties.MessageProperties.Length==0||_properties.MessageProperties[0].CaptureType==CaptureType.Unknown;
845844
}
846845
}
847846
}

‎tests/NLog.UnitTests/LoggerTests.cs‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,12 +1360,11 @@ public void ConditionalTraceTest()
13601360
logger.ConditionalTrace(NLCulture,"we've got error {0}, {1}, {2}, {3} ...",500,501,502,503);
13611361
if(enabled==1)AssertDebugLastMessage("debug","A|we've got error 500, 501, 502, 503 ...");
13621362

1363-
13641363
logger.ConditionalTrace(argException,NLCulture,"we've got error {0}, {1}, {2}, {3} ...",500,501,502,503.5);
1365-
if(enabled==1)AssertDebugLastMessage("debug","A|we\'ve got error 500, 501, 502, 503,5 ...arg1 is obvious wrong\r\nParameter name: arg1");
1364+
if(enabled==1)AssertDebugLastMessage("debug",$"A|we\'ve got error 500, 501, 502, 503,5 ...{argException.Message}");
13661365

13671366
logger.ConditionalTrace(argException,"we've got error {0}, {1}, {2}, {3} ...",500,501,502,503.5);
1368-
if(enabled==1)AssertDebugLastMessage("debug","A|we\'ve got error 500, 501, 502, 503.5 ...arg1 is obvious wrong\r\nParameter name: arg1");
1367+
if(enabled==1)AssertDebugLastMessage("debug",$"A|we\'ve got error 500, 501, 502, 503.5 ...{argException.Message}");
13691368

13701369
logger.ConditionalTrace("message{0}",(ulong)1);
13711370
if(enabled==1)AssertDebugLastMessage("debug","A|message1");
@@ -1534,12 +1533,11 @@ public void ConditionalTraceILogTest()
15341533
logger.ConditionalTrace(NLCulture,"we've got error {0}, {1}, {2}, {3} ...",500,501,502,503);
15351534
if(enabled==1)AssertDebugLastMessage("debug","A|we've got error 500, 501, 502, 503 ...");
15361535

1537-
15381536
logger.ConditionalTrace(argException,NLCulture,"we've got error {0}, {1}, {2}, {3} ...",500,501,502,503.5);
1539-
if(enabled==1)AssertDebugLastMessage("debug","A|we\'ve got error 500, 501, 502, 503,5 ...arg1 is obvious wrong\r\nParameter name: arg1");
1537+
if(enabled==1)AssertDebugLastMessage("debug",$"A|we\'ve got error 500, 501, 502, 503,5 ...{argException.Message}");
15401538

15411539
logger.ConditionalTrace(argException,"we've got error {0}, {1}, {2}, {3} ...",500,501,502,503.5);
1542-
if(enabled==1)AssertDebugLastMessage("debug","A|we\'ve got error 500, 501, 502, 503.5 ...arg1 is obvious wrong\r\nParameter name: arg1");
1540+
if(enabled==1)AssertDebugLastMessage("debug",$"A|we\'ve got error 500, 501, 502, 503.5 ...{argException.Message}");
15431541

15441542
logger.ConditionalTrace("message{0}",(ulong)1);
15451543
if(enabled==1)AssertDebugLastMessage("debug","A|message1");
@@ -1707,12 +1705,11 @@ public void ConditionalDebugTest()
17071705
logger.ConditionalDebug(NLCulture,"we've got error {0}, {1}, {2}, {3} ...",500,501,502,503);
17081706
if(enabled==1)AssertDebugLastMessage("debug","A|we've got error 500, 501, 502, 503 ...");
17091707

1710-
17111708
logger.ConditionalDebug(argException,NLCulture,"we've got error {0}, {1}, {2}, {3} ...",500,501,502,503.5);
1712-
if(enabled==1)AssertDebugLastMessage("debug","A|we\'ve got error 500, 501, 502, 503,5 ...arg1 is obvious wrong\r\nParameter name: arg1");
1709+
if(enabled==1)AssertDebugLastMessage("debug",$"A|we\'ve got error 500, 501, 502, 503,5 ...{argException.Message}");
17131710

17141711
logger.ConditionalDebug(argException,"we've got error {0}, {1}, {2}, {3} ...",500,501,502,503.5);
1715-
if(enabled==1)AssertDebugLastMessage("debug","A|we\'ve got error 500, 501, 502, 503.5 ...arg1 is obvious wrong\r\nParameter name: arg1");
1712+
if(enabled==1)AssertDebugLastMessage("debug",$"A|we\'ve got error 500, 501, 502, 503.5 ...{argException.Message}");
17161713

17171714
logger.ConditionalDebug("message{0}",(ulong)1);
17181715
if(enabled==1)AssertDebugLastMessage("debug","A|message1");
@@ -1882,10 +1879,10 @@ public void ConditionalDebugILogTest()
18821879
if(enabled==1)AssertDebugLastMessage("debug","A|we've got error 500, 501, 502, 503 ...");
18831880

18841881
logger.ConditionalDebug(argException,NLCulture,"we've got error {0}, {1}, {2}, {3} ...",500,501,502,503.5);
1885-
if(enabled==1)AssertDebugLastMessage("debug","A|we\'ve got error 500, 501, 502, 503,5 ...arg1 is obvious wrong\r\nParameter name: arg1");
1882+
if(enabled==1)AssertDebugLastMessage("debug",$"A|we\'ve got error 500, 501, 502, 503,5 ...{argException.Message}");
18861883

18871884
logger.ConditionalDebug(argException,"we've got error {0}, {1}, {2}, {3} ...",500,501,502,503.5);
1888-
if(enabled==1)AssertDebugLastMessage("debug","A|we\'ve got error 500, 501, 502, 503.5 ...arg1 is obvious wrong\r\nParameter name: arg1");
1885+
if(enabled==1)AssertDebugLastMessage("debug",$"A|we\'ve got error 500, 501, 502, 503.5 ...{argException.Message}");
18891886

18901887
logger.ConditionalDebug("message{0}",(ulong)1);
18911888
if(enabled==1)AssertDebugLastMessage("debug","A|message1");

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp