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

Commita9f0f23

Browse files
committed
Reduce code complexity
1 parent9d427f9 commita9f0f23

File tree

4 files changed

+350
-130
lines changed

4 files changed

+350
-130
lines changed

‎src/NLog/Internal/PropertiesDictionary.cs‎

Lines changed: 50 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ 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
{
52+
constintSmallArraySize=3;
53+
5254
private
5355
#if!NETFRAMEWORK
5456
readonly
@@ -99,7 +101,7 @@ public PropertiesDictionary(MessageTemplateParameter[]? messageParameters = null
99101
/// </summary>
100102
publicPropertiesDictionary(intinitialCapacity)
101103
{
102-
if(initialCapacity>3)
104+
if(initialCapacity>SmallArraySize)
103105
{
104106
_eventProperties=newDictionary<object,PropertyValue>(initialCapacity,PropertyKeyComparer.Default);
105107
}
@@ -121,7 +123,7 @@ private Dictionary<object, PropertyValue> GetEventProperties(bool prepareForInse
121123
return_eventProperties;
122124
}
123125

124-
publicIList<MessageTemplateParameter>MessageProperties=>this;
126+
publicMessageTemplateParameter[]MessageProperties=>(_messagePropertiesisnull||(_messagePropertiesCount==SmallArraySize&&_messageProperties[0].CaptureType==CaptureType.Unknown))?ArrayHelper.Empty<MessageTemplateParameter>():_messageProperties;
125127

126128
publicvoidResetMessageProperties(MessageTemplateParameter[]?newMessageProperties=null,intnewMessagePropertiesCount=0)
127129
{
@@ -134,7 +136,7 @@ public void ResetMessageProperties(MessageTemplateParameter[]? newMessagePropert
134136
{
135137
eventProperties=_eventProperties=oldMessagePropertiesCount==0?
136138
newDictionary<object,PropertyValue>(newMessagePropertiesCount,PropertyKeyComparer.Default):
137-
InitializeEventPropertiesDictionary(false,oldMessageProperties,oldMessagePropertiesCount,outvar_);
139+
InitializeEventPropertiesDictionary(newMessagePropertiesCount>oldMessagePropertiesCount,oldMessageProperties,oldMessagePropertiesCount,outvar_);
138140
}
139141

140142
if(oldMessageProperties!=null&&eventProperties.Count>0)
@@ -146,13 +148,18 @@ public void ResetMessageProperties(MessageTemplateParameter[]? newMessagePropert
146148
{
147149
InsertMessagePropertiesIntoEmptyDictionary(eventProperties,newMessageProperties,newMessagePropertiesCount,out_);
148150
}
149-
}
150151

151-
_messageProperties=newMessageProperties;
152-
_messagePropertiesCount=newMessagePropertiesCount;
152+
_messageProperties=newMessageProperties;
153+
_messagePropertiesCount=newMessagePropertiesCount;
154+
}
155+
elseif(newMessagePropertiesCount>0)
156+
{
157+
_messageProperties=newMessageProperties;
158+
_messagePropertiesCount=newMessagePropertiesCount;
159+
}
153160
}
154161

155-
privatestaticvoidRemoveOldMessageProperties(Dictionary<object,PropertyValue>eventProperties,IList<MessageTemplateParameter>oldMessageProperties,intoldMessagePropertiesCount)
162+
privatestaticvoidRemoveOldMessageProperties(Dictionary<object,PropertyValue>eventProperties,MessageTemplateParameter[]oldMessageProperties,intoldMessagePropertiesCount)
156163
{
157164
for(inti=0;i<oldMessagePropertiesCount;++i)
158165
{
@@ -165,7 +172,7 @@ private static void RemoveOldMessageProperties(Dictionary<object, PropertyValue>
165172

166173
privatestaticDictionary<object,PropertyValue>InitializeEventPropertiesDictionary(boolprepareForInsert,MessageTemplateParameter[]?messageProperties,intmessagePropertiesCount,outboolresetMessageProperties)
167174
{
168-
if(messageProperties!=null&&messagePropertiesCount>0)
175+
if(messagePropertiesCount>0&&messageProperties!=null)
169176
{
170177
vardictionaryCapacity=prepareForInsert?(messagePropertiesCount+2):messagePropertiesCount;
171178
vareventProperties=newDictionary<object,PropertyValue>(dictionaryCapacity,PropertyKeyComparer.Default);
@@ -195,26 +202,6 @@ private static Dictionary<object, PropertyValue> InitializeEventPropertiesDictio
195202
/// <inheritDoc/>
196203
publicboolIsReadOnly=>false;
197204

198-
intICollection<MessageTemplateParameter>.Count=>_messagePropertiesCount;
199-
200-
boolICollection<MessageTemplateParameter>.IsReadOnly=>true;
201-
202-
MessageTemplateParameterIList<MessageTemplateParameter>.this[intindex]
203-
{
204-
get
205-
{
206-
if(index>=_messagePropertiesCount||index<0||_messagePropertiesisnull)
207-
thrownewArgumentOutOfRangeException(nameof(index));
208-
return_messageProperties[index];
209-
}
210-
set
211-
{
212-
if(index>=_messagePropertiesCount||index<0||_messagePropertiesisnull)
213-
thrownewArgumentOutOfRangeException(nameof(index));
214-
_messageProperties[index]=value;
215-
}
216-
}
217-
218205
/// <inheritDoc/>
219206
publicobject?this[objectkey]
220207
{
@@ -231,16 +218,7 @@ public object? this[object key]
231218
{
232219
if(SkipDictionaryAllocation()&&keyisstringpropertyName&&propertyName.Length>0)
233220
{
234-
varmessageProperties=_messageProperties??(_messageProperties=newMessageTemplateParameter[3]);
235-
for(inti=0;i<_messagePropertiesCount;++i)
236-
{
237-
if(messageProperties[i].Name.Equals(propertyName))
238-
{
239-
messageProperties[i]=newMessageTemplateParameter(messageProperties[i].Name,value,messageProperties[i].Format,messageProperties[i].CaptureType);
240-
return;
241-
}
242-
}
243-
messageProperties[_messagePropertiesCount++]=newMessageTemplateParameter(propertyName,value,null,CaptureType.Unknown);
221+
AddOrUpdateMessageProperties(propertyName,value,true);
244222
return;
245223
}
246224

@@ -253,26 +231,13 @@ public void Add(object key, object? value)
253231
{
254232
if(SkipDictionaryAllocation()&&keyisstringpropertyName&&propertyName.Length>0)
255233
{
256-
varmessageProperties=_messageProperties??(_messageProperties=newMessageTemplateParameter[3]);
257-
for(inti=0;i<_messagePropertiesCount;++i)
258-
{
259-
if(messageProperties[i].Name.Equals(propertyName))
260-
{
261-
thrownewArgumentException($"An item with the same key{propertyName} has already been added.",nameof(key));
262-
}
263-
}
264-
messageProperties[_messagePropertiesCount++]=newMessageTemplateParameter(propertyName,value,null,CaptureType.Unknown);
234+
AddOrUpdateMessageProperties(propertyName,value,false);
265235
return;
266236
}
267237

268238
GetEventProperties(true)[key]=newPropertyValue(value,false);
269239
}
270240

271-
privateboolSkipDictionaryAllocation()
272-
{
273-
return_eventPropertiesisnull&&(_messagePropertiesisnull||_messagePropertiesCount<_messageProperties.Length);
274-
}
275-
276241
/// <inheritDoc/>
277242
publicvoidAdd(KeyValuePair<object,object?>item)
278243
{
@@ -370,18 +335,15 @@ public bool Remove(KeyValuePair<object, object?> item)
370335
/// <inheritDoc/>
371336
publicboolTryGetValue(objectkey,outobject?value)
372337
{
373-
if(!IsEmpty)
338+
if(_eventPropertiesisnull)
374339
{
375-
if(_eventPropertiesisnull)
376-
{
377-
returnTryLookupMessagePropertyValue(key,outvalue);
378-
}
340+
returnTryLookupMessagePropertyValue(key,outvalue);
341+
}
379342

380-
if(_eventProperties.TryGetValue(key,outvareventProperty))
381-
{
382-
value=eventProperty.Value;
383-
returntrue;
384-
}
343+
if(_eventProperties.TryGetValue(key,outvareventProperty))
344+
{
345+
value=eventProperty.Value;
346+
returntrue;
385347
}
386348

387349
value=null;
@@ -431,6 +393,30 @@ private bool TryLookupMessagePropertyValue(object key, out object? propertyValue
431393
returnfalse;
432394
}
433395

396+
privatevoidAddOrUpdateMessageProperties(stringpropertyName,object?value,boolallowUpdate)
397+
{
398+
varmessageProperties=_messageProperties??(_messageProperties=newMessageTemplateParameter[SmallArraySize]);
399+
for(inti=0;i<_messagePropertiesCount;++i)
400+
{
401+
if(propertyName.Equals(messageProperties[i].Name))
402+
{
403+
if(!allowUpdate)
404+
{
405+
thrownewArgumentException($"An item with the same key{propertyName} has already been added.",nameof(propertyName));
406+
}
407+
messageProperties[i]=newMessageTemplateParameter(propertyName,value,null,CaptureType.Unknown);
408+
return;
409+
}
410+
}
411+
412+
messageProperties[_messagePropertiesCount++]=newMessageTemplateParameter(propertyName,value,null,CaptureType.Unknown);
413+
}
414+
415+
privateboolSkipDictionaryAllocation()
416+
{
417+
return_eventPropertiesisnull&&(_messagePropertiesisnull||_messagePropertiesCount<_messageProperties.Length);
418+
}
419+
434420
/// <summary>
435421
/// Check if the message-template-parameters can be used directly without allocating a dictionary
436422
/// </summary>
@@ -513,65 +499,6 @@ internal static string GenerateUniquePropertyName<TKey, TValue>(string originalN
513499
returnnewItemName;
514500
}
515501

516-
intIList<MessageTemplateParameter>.IndexOf(MessageTemplateParameteritem)
517-
{
518-
if(_messageProperties!=null&&_messagePropertiesCount>0)
519-
{
520-
for(inti=0;i<_messagePropertiesCount;++i)
521-
{
522-
if(_messageProperties[i].Equals(item))
523-
returni;
524-
}
525-
}
526-
return-1;
527-
}
528-
529-
boolICollection<MessageTemplateParameter>.Contains(MessageTemplateParameteritem)
530-
{
531-
return((IList<MessageTemplateParameter>)this).IndexOf(item)>=0;
532-
}
533-
534-
voidICollection<MessageTemplateParameter>.CopyTo(MessageTemplateParameter[]array,intarrayIndex)
535-
{
536-
if(_messageProperties!=null&&_messagePropertiesCount>0)
537-
{
538-
Array.Copy(_messageProperties,0,array,arrayIndex,_messagePropertiesCount);
539-
}
540-
}
541-
542-
voidIList<MessageTemplateParameter>.Insert(intindex,MessageTemplateParameteritem)
543-
{
544-
thrownewNotSupportedException("MessageTemplateParameters array is read-only");
545-
}
546-
547-
voidIList<MessageTemplateParameter>.RemoveAt(intindex)
548-
{
549-
thrownewNotSupportedException("MessageTemplateParameters array is read-only");
550-
}
551-
552-
voidICollection<MessageTemplateParameter>.Add(MessageTemplateParameteritem)
553-
{
554-
thrownewNotSupportedException("MessageTemplateParameters array is read-only");
555-
}
556-
557-
voidICollection<MessageTemplateParameter>.Clear()
558-
{
559-
thrownewNotSupportedException("MessageTemplateParameters array is read-only");
560-
}
561-
562-
boolICollection<MessageTemplateParameter>.Remove(MessageTemplateParameteritem)
563-
{
564-
thrownewNotSupportedException("MessageTemplateParameters array is read-only");
565-
}
566-
567-
IEnumerator<MessageTemplateParameter>IEnumerable<MessageTemplateParameter>.GetEnumerator()
568-
{
569-
if(_messagePropertiesisnull)
570-
returnSystem.Linq.Enumerable.Empty<MessageTemplateParameter>().GetEnumerator();
571-
else
572-
return((IList<MessageTemplateParameter>)_messageProperties).GetEnumerator();
573-
}
574-
575502
publicstructPropertyDictionaryEnumerator:IEnumerator<KeyValuePair<object,object?>>
576503
{
577504
privatereadonlyPropertiesDictionary_dictionary;
@@ -699,7 +626,7 @@ public void Dispose()
699626
publicvoidReset()
700627
{
701628
_messagePropertiesIndex=_dictionary._messagePropertiesCount>0?-1:default(int?);
702-
_eventEnumerator=default(Dictionary<object,PropertyValue>.Enumerator);
629+
_eventEnumerator=_dictionary._eventProperties?.GetEnumerator()??default(Dictionary<object,PropertyValue>.Enumerator);
703630
}
704631
}
705632

‎src/NLog/LogEventInfo.cs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,10 @@ public MessageTemplateParameters MessageTemplateParameters
465465
{
466466
get
467467
{
468-
if(_properties?.MessageProperties.Count>0)
468+
varmessageProperties=_properties?.MessageProperties;
469+
if(messageProperties?.Length>0)
469470
{
470-
returnnewMessageTemplateParameters(_properties.MessageProperties);
471+
returnnewMessageTemplateParameters(messageProperties);
471472
}
472473
elseif(_parameters?.Length>0)
473474
{
@@ -711,7 +712,7 @@ internal bool IsLogEventThreadAgnosticImmutable()
711712
if(properties.Count>5)
712713
returnfalse;// too many properties, too costly to check
713714

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

717718
returnHasImmutableProperties(properties);
@@ -720,12 +721,11 @@ internal bool IsLogEventThreadAgnosticImmutable()
720721
privatestaticboolHasImmutableProperties(PropertiesDictionaryproperties)
721722
{
722723
varmessageProperties=properties.MessageProperties;
723-
if(properties.Count==messageProperties.Count)
724+
if(properties.Count==messageProperties.Length)
724725
{
725726
// Skip enumerator allocation when all properties comes from the message-template
726-
for(inti=0;i<messageProperties.Count;++i)
727+
foreach(varpropertyinmessageProperties)
727728
{
728-
varproperty=messageProperties[i];
729729
if(!IsSafeToDeferFormatting(property.Value))
730730
returnfalse;
731731
}
@@ -841,7 +841,7 @@ private bool ResetMessageTemplateParameters()
841841
}
842842

843843
// 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;
844+
return_properties.MessageProperties.Length==0;
845845
}
846846
}
847847
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp