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

Commitd9b3a62

Browse files
committed
Reduce code complexity
1 parent9d427f9 commitd9b3a62

File tree

2 files changed

+36
-113
lines changed

2 files changed

+36
-113
lines changed

‎src/NLog/Internal/PropertiesDictionary.cs‎

Lines changed: 30 additions & 107 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=>_messagePropertiesCount>0&&_messageProperties?.Length==_messagePropertiesCount?_messageProperties:ArrayHelper.Empty<MessageTemplateParameter>();
125125

126126
publicvoidResetMessageProperties(MessageTemplateParameter[]?newMessageProperties=null,intnewMessagePropertiesCount=0)
127127
{
@@ -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
{
@@ -431,6 +389,30 @@ private bool TryLookupMessagePropertyValue(object key, out object? propertyValue
431389
returnfalse;
432390
}
433391

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

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-
575498
publicstructPropertyDictionaryEnumerator:IEnumerator<KeyValuePair<object,object?>>
576499
{
577500
privatereadonlyPropertiesDictionary_dictionary;

‎src/NLog/LogEventInfo.cs‎

Lines changed: 6 additions & 6 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,7 +711,7 @@ 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);
@@ -720,12 +720,11 @@ internal bool IsLogEventThreadAgnosticImmutable()
720720
privatestaticboolHasImmutableProperties(PropertiesDictionaryproperties)
721721
{
722722
varmessageProperties=properties.MessageProperties;
723-
if(properties.Count==messageProperties.Count)
723+
if(properties.Count==messageProperties.Length)
724724
{
725725
// Skip enumerator allocation when all properties comes from the message-template
726-
for(inti=0;i<messageProperties.Count;++i)
726+
foreach(varpropertyinmessageProperties)
727727
{
728-
varproperty=messageProperties[i];
729728
if(!IsSafeToDeferFormatting(property.Value))
730729
returnfalse;
731730
}
@@ -841,7 +840,8 @@ 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+
varmessageProperties=_properties.MessageProperties;
844+
returnmessageProperties.Length==0||messageProperties[0].CaptureType==CaptureType.Unknown;
845845
}
846846
}
847847
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp