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

Commit8937426

Browse files
committed
Reduce code complexity
1 parent9d427f9 commit8937426

File tree

3 files changed

+320
-126
lines changed

3 files changed

+320
-126
lines changed

‎src/NLog/Internal/PropertiesDictionary.cs‎

Lines changed: 39 additions & 119 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!=_messageProperties.Length)?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)
@@ -152,7 +152,7 @@ public void ResetMessageProperties(MessageTemplateParameter[]? newMessagePropert
152152
_messagePropertiesCount=newMessagePropertiesCount;
153153
}
154154

155-
privatestaticvoidRemoveOldMessageProperties(Dictionary<object,PropertyValue>eventProperties,IList<MessageTemplateParameter>oldMessageProperties,intoldMessagePropertiesCount)
155+
privatestaticvoidRemoveOldMessageProperties(Dictionary<object,PropertyValue>eventProperties,MessageTemplateParameter[]oldMessageProperties,intoldMessagePropertiesCount)
156156
{
157157
for(inti=0;i<oldMessagePropertiesCount;++i)
158158
{
@@ -165,7 +165,7 @@ private static void RemoveOldMessageProperties(Dictionary<object, PropertyValue>
165165

166166
privatestaticDictionary<object,PropertyValue>InitializeEventPropertiesDictionary(boolprepareForInsert,MessageTemplateParameter[]?messageProperties,intmessagePropertiesCount,outboolresetMessageProperties)
167167
{
168-
if(messageProperties!=null&&messagePropertiesCount>0)
168+
if(messagePropertiesCount>0&&messageProperties!=null)
169169
{
170170
vardictionaryCapacity=prepareForInsert?(messagePropertiesCount+2):messagePropertiesCount;
171171
vareventProperties=newDictionary<object,PropertyValue>(dictionaryCapacity,PropertyKeyComparer.Default);
@@ -195,26 +195,6 @@ private static Dictionary<object, PropertyValue> InitializeEventPropertiesDictio
195195
/// <inheritDoc/>
196196
publicboolIsReadOnly=>false;
197197

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-
218198
/// <inheritDoc/>
219199
publicobject?this[objectkey]
220200
{
@@ -231,16 +211,7 @@ public object? this[object key]
231211
{
232212
if(SkipDictionaryAllocation()&&keyisstringpropertyName&&propertyName.Length>0)
233213
{
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);
214+
AddOrUpdateMessageProperties(propertyName,value,true);
244215
return;
245216
}
246217

@@ -253,26 +224,13 @@ public void Add(object key, object? value)
253224
{
254225
if(SkipDictionaryAllocation()&&keyisstringpropertyName&&propertyName.Length>0)
255226
{
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);
227+
AddOrUpdateMessageProperties(propertyName,value,false);
265228
return;
266229
}
267230

268231
GetEventProperties(true)[key]=newPropertyValue(value,false);
269232
}
270233

271-
privateboolSkipDictionaryAllocation()
272-
{
273-
return_eventPropertiesisnull&&(_messagePropertiesisnull||_messagePropertiesCount<_messageProperties.Length);
274-
}
275-
276234
/// <inheritDoc/>
277235
publicvoidAdd(KeyValuePair<object,object?>item)
278236
{
@@ -370,18 +328,15 @@ public bool Remove(KeyValuePair<object, object?> item)
370328
/// <inheritDoc/>
371329
publicboolTryGetValue(objectkey,outobject?value)
372330
{
373-
if(!IsEmpty)
331+
if(_eventPropertiesisnull)
374332
{
375-
if(_eventPropertiesisnull)
376-
{
377-
returnTryLookupMessagePropertyValue(key,outvalue);
378-
}
333+
returnTryLookupMessagePropertyValue(key,outvalue);
334+
}
379335

380-
if(_eventProperties.TryGetValue(key,outvareventProperty))
381-
{
382-
value=eventProperty.Value;
383-
returntrue;
384-
}
336+
if(_eventProperties.TryGetValue(key,outvareventProperty))
337+
{
338+
value=eventProperty.Value;
339+
returntrue;
385340
}
386341

387342
value=null;
@@ -431,6 +386,30 @@ private bool TryLookupMessagePropertyValue(object key, out object? propertyValue
431386
returnfalse;
432387
}
433388

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

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-
575495
publicstructPropertyDictionaryEnumerator:IEnumerator<KeyValuePair<object,object?>>
576496
{
577497
privatereadonlyPropertiesDictionary_dictionary;
@@ -699,7 +619,7 @@ public void Dispose()
699619
publicvoidReset()
700620
{
701621
_messagePropertiesIndex=_dictionary._messagePropertiesCount>0?-1:default(int?);
702-
_eventEnumerator=default(Dictionary<object,PropertyValue>.Enumerator);
622+
_eventEnumerator=_dictionary._eventProperties?.GetEnumerator()??default(Dictionary<object,PropertyValue>.Enumerator);
703623
}
704624
}
705625

‎src/NLog/LogEventInfo.cs‎

Lines changed: 8 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,8 @@ 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+
varmessageProperties=_properties.MessageProperties;
845+
returnmessageProperties.Length==0||messageProperties[0].CaptureType==CaptureType.Unknown;
845846
}
846847
}
847848
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp