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

Commit219f411

Browse files
committed
Reduce code complexity
1 parentc361f1c commit219f411

File tree

4 files changed

+427
-132
lines changed

4 files changed

+427
-132
lines changed

‎src/NLog/Internal/PropertiesDictionary.cs‎

Lines changed: 54 additions & 125 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||(_messageProperties.Length==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
{
@@ -229,48 +216,14 @@ public object? this[object key]
229216
}
230217
set
231218
{
232-
if(SkipDictionaryAllocation()&&keyisstringpropertyName&&propertyName.Length>0)
233-
{
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);
244-
return;
245-
}
246-
247-
GetEventProperties(true)[key]=newPropertyValue(value,false);
219+
AddOrUpdateMessageProperties(key,value,true);
248220
}
249221
}
250222

251223
/// <inheritDoc/>
252224
publicvoidAdd(objectkey,object?value)
253225
{
254-
if(SkipDictionaryAllocation()&&keyisstringpropertyName&&propertyName.Length>0)
255-
{
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);
265-
return;
266-
}
267-
268-
GetEventProperties(true)[key]=newPropertyValue(value,false);
269-
}
270-
271-
privateboolSkipDictionaryAllocation()
272-
{
273-
return_eventPropertiesisnull&&(_messagePropertiesisnull||_messagePropertiesCount<_messageProperties.Length);
226+
AddOrUpdateMessageProperties(key,value,false);
274227
}
275228

276229
/// <inheritDoc/>
@@ -428,6 +381,41 @@ private bool TryLookupMessagePropertyValue(object key, out object? propertyValue
428381
returnfalse;
429382
}
430383

384+
privatevoidAddOrUpdateMessageProperties(objectkey,object?value,boolallowUpdate)
385+
{
386+
if(SkipDictionaryAllocation()&&keyisstringpropertyName&&propertyName.Length>0)
387+
{
388+
varmessageProperties=_messageProperties??(_messageProperties=newMessageTemplateParameter[SmallArraySize]);
389+
for(inti=0;i<_messagePropertiesCount;++i)
390+
{
391+
if(propertyName.Equals(messageProperties[i].Name))
392+
{
393+
if(!allowUpdate)
394+
{
395+
thrownewArgumentException($"An item with the same key{propertyName} has already been added.",nameof(key));
396+
}
397+
messageProperties[i]=newMessageTemplateParameter(propertyName,value,null,CaptureType.Unknown);
398+
return;
399+
}
400+
}
401+
402+
messageProperties[_messagePropertiesCount++]=newMessageTemplateParameter(propertyName,value,null,CaptureType.Unknown);
403+
}
404+
elseif(allowUpdate)
405+
{
406+
GetEventProperties(true)[key]=newPropertyValue(value,false);
407+
}
408+
else
409+
{
410+
GetEventProperties(true).Add(key,newPropertyValue(value,false));
411+
}
412+
}
413+
414+
privateboolSkipDictionaryAllocation()
415+
{
416+
return_eventPropertiesisnull&&(_messagePropertiesisnull||_messagePropertiesCount<_messageProperties.Length);
417+
}
418+
431419
/// <summary>
432420
/// Check if the message-template-parameters can be used directly without allocating a dictionary
433421
/// </summary>
@@ -510,65 +498,6 @@ internal static string GenerateUniquePropertyName<TKey, TValue>(string originalN
510498
returnnewItemName;
511499
}
512500

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

‎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