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

Commit03bc819

Browse files
committed
Reduce code complexity
1 parent9d427f9 commit03bc819

File tree

4 files changed

+347
-129
lines changed

4 files changed

+347
-129
lines changed

‎src/NLog/Internal/PropertiesDictionary.cs‎

Lines changed: 47 additions & 122 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
@@ -121,7 +121,7 @@ private Dictionary<object, PropertyValue> GetEventProperties(bool prepareForInse
121121
return_eventProperties;
122122
}
123123

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

126126
publicvoidResetMessageProperties(MessageTemplateParameter[]?newMessageProperties=null,intnewMessagePropertiesCount=0)
127127
{
@@ -134,7 +134,7 @@ public void ResetMessageProperties(MessageTemplateParameter[]? newMessagePropert
134134
{
135135
eventProperties=_eventProperties=oldMessagePropertiesCount==0?
136136
newDictionary<object,PropertyValue>(newMessagePropertiesCount,PropertyKeyComparer.Default):
137-
InitializeEventPropertiesDictionary(false,oldMessageProperties,oldMessagePropertiesCount,outvar_);
137+
InitializeEventPropertiesDictionary(newMessagePropertiesCount>oldMessagePropertiesCount,oldMessageProperties,oldMessagePropertiesCount,outvar_);
138138
}
139139

140140
if(oldMessageProperties!=null&&eventProperties.Count>0)
@@ -146,13 +146,18 @@ public void ResetMessageProperties(MessageTemplateParameter[]? newMessagePropert
146146
{
147147
InsertMessagePropertiesIntoEmptyDictionary(eventProperties,newMessageProperties,newMessagePropertiesCount,out_);
148148
}
149-
}
150149

151-
_messageProperties=newMessageProperties;
152-
_messagePropertiesCount=newMessagePropertiesCount;
150+
_messageProperties=newMessageProperties;
151+
_messagePropertiesCount=newMessagePropertiesCount;
152+
}
153+
elseif(newMessagePropertiesCount>0)
154+
{
155+
_messageProperties=newMessageProperties;
156+
_messagePropertiesCount=newMessagePropertiesCount;
157+
}
153158
}
154159

155-
privatestaticvoidRemoveOldMessageProperties(Dictionary<object,PropertyValue>eventProperties,IList<MessageTemplateParameter>oldMessageProperties,intoldMessagePropertiesCount)
160+
privatestaticvoidRemoveOldMessageProperties(Dictionary<object,PropertyValue>eventProperties,MessageTemplateParameter[]oldMessageProperties,intoldMessagePropertiesCount)
156161
{
157162
for(inti=0;i<oldMessagePropertiesCount;++i)
158163
{
@@ -165,7 +170,7 @@ private static void RemoveOldMessageProperties(Dictionary<object, PropertyValue>
165170

166171
privatestaticDictionary<object,PropertyValue>InitializeEventPropertiesDictionary(boolprepareForInsert,MessageTemplateParameter[]?messageProperties,intmessagePropertiesCount,outboolresetMessageProperties)
167172
{
168-
if(messageProperties!=null&&messagePropertiesCount>0)
173+
if(messagePropertiesCount>0&&messageProperties!=null)
169174
{
170175
vardictionaryCapacity=prepareForInsert?(messagePropertiesCount+2):messagePropertiesCount;
171176
vareventProperties=newDictionary<object,PropertyValue>(dictionaryCapacity,PropertyKeyComparer.Default);
@@ -195,26 +200,6 @@ private static Dictionary<object, PropertyValue> InitializeEventPropertiesDictio
195200
/// <inheritDoc/>
196201
publicboolIsReadOnly=>false;
197202

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-
218203
/// <inheritDoc/>
219204
publicobject?this[objectkey]
220205
{
@@ -231,16 +216,7 @@ public object? this[object key]
231216
{
232217
if(SkipDictionaryAllocation()&&keyisstringpropertyName&&propertyName.Length>0)
233218
{
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);
219+
AddOrUpdateMessageProperties(propertyName,value,true);
244220
return;
245221
}
246222

@@ -253,26 +229,13 @@ public void Add(object key, object? value)
253229
{
254230
if(SkipDictionaryAllocation()&&keyisstringpropertyName&&propertyName.Length>0)
255231
{
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);
232+
AddOrUpdateMessageProperties(propertyName,value,false);
265233
return;
266234
}
267235

268236
GetEventProperties(true)[key]=newPropertyValue(value,false);
269237
}
270238

271-
privateboolSkipDictionaryAllocation()
272-
{
273-
return_eventPropertiesisnull&&(_messagePropertiesisnull||_messagePropertiesCount<_messageProperties.Length);
274-
}
275-
276239
/// <inheritDoc/>
277240
publicvoidAdd(KeyValuePair<object,object?>item)
278241
{
@@ -370,18 +333,15 @@ public bool Remove(KeyValuePair<object, object?> item)
370333
/// <inheritDoc/>
371334
publicboolTryGetValue(objectkey,outobject?value)
372335
{
373-
if(!IsEmpty)
336+
if(_eventPropertiesisnull)
374337
{
375-
if(_eventPropertiesisnull)
376-
{
377-
returnTryLookupMessagePropertyValue(key,outvalue);
378-
}
338+
returnTryLookupMessagePropertyValue(key,outvalue);
339+
}
379340

380-
if(_eventProperties.TryGetValue(key,outvareventProperty))
381-
{
382-
value=eventProperty.Value;
383-
returntrue;
384-
}
341+
if(_eventProperties.TryGetValue(key,outvareventProperty))
342+
{
343+
value=eventProperty.Value;
344+
returntrue;
385345
}
386346

387347
value=null;
@@ -431,6 +391,30 @@ private bool TryLookupMessagePropertyValue(object key, out object? propertyValue
431391
returnfalse;
432392
}
433393

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

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-
575500
publicstructPropertyDictionaryEnumerator:IEnumerator<KeyValuePair<object,object?>>
576501
{
577502
privatereadonlyPropertiesDictionary_dictionary;
@@ -699,7 +624,7 @@ public void Dispose()
699624
publicvoidReset()
700625
{
701626
_messagePropertiesIndex=_dictionary._messagePropertiesCount>0?-1:default(int?);
702-
_eventEnumerator=default(Dictionary<object,PropertyValue>.Enumerator);
627+
_eventEnumerator=_dictionary._eventProperties?.GetEnumerator()??default(Dictionary<object,PropertyValue>.Enumerator);
703628
}
704629
}
705630

‎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