@@ -11,7 +11,7 @@ namespace NLog.LayoutRenderers
1111 [ LayoutRenderer ( "activity" ) ]
1212 public sealed class ActivityTraceLayoutRenderer : LayoutRenderer
1313 {
14- private static string [ ] DurationMsFormat = null ;
14+ private static string [ ] ? DurationMsFormat = null ;
1515
1616 /// <summary>
1717 /// Gets or sets the property to retrieve.
@@ -22,12 +22,12 @@ public sealed class ActivityTraceLayoutRenderer : LayoutRenderer
2222 /// <summary>
2323 /// Single item to extract from <see cref="System.Diagnostics.Activity.Baggage"/> or <see cref="System.Diagnostics.Activity.Tags"/> or with <see cref="System.Diagnostics.Activity.GetCustomProperty(string)"/>
2424 /// </summary>
25- public string Item { get ; set ; }
25+ public string ? Item { get ; set ; }
2626
2727 /// <summary>
2828 /// Control output formating of selected property (if supported)
2929 /// </summary>
30- public string Format { get ; set ; }
30+ public string ? Format { get ; set ; }
3131
3232 /// <summary>
3333 /// Gets or sets the culture used for rendering.
@@ -167,7 +167,7 @@ private void RenderDurationMs(StringBuilder builder, System.Diagnostics.Activity
167167 }
168168 }
169169
170- private string GetValue ( System . Diagnostics . Activity activity )
170+ private string ? GetValue ( System . Diagnostics . Activity activity )
171171 {
172172 switch ( Property )
173173 {
@@ -183,7 +183,7 @@ private string GetValue(System.Diagnostics.Activity activity)
183183 case ActivityTraceProperty . TraceFlags : return ConvertToString ( activity . ActivityTraceFlags , Format ) ;
184184 case ActivityTraceProperty . Baggage : return GetCollectionItem ( Item , activity . Baggage ) ;
185185 case ActivityTraceProperty . Tags : return GetCollectionItem ( Item , activity . TagObjects ) ;
186- case ActivityTraceProperty . CustomProperty : return string . IsNullOrEmpty ( Item ) ? string . Empty : ( activity . GetCustomProperty ( Item ) ? . ToString ( ) ?? activity . Parent ? . GetCustomProperty ( Item ) ? . ToString ( ) ?? string . Empty ) ;
186+ case ActivityTraceProperty . CustomProperty : return GetCustomProperty ( Item , activity ) ;
187187 case ActivityTraceProperty . SourceName : return activity . Source ? . Name ;
188188 case ActivityTraceProperty . SourceVersion : return activity . Source ? . Version ;
189189 case ActivityTraceProperty . ActivityKind : return ConvertToString ( activity . Kind , Format ) ;
@@ -195,6 +195,14 @@ private string GetValue(System.Diagnostics.Activity activity)
195195 }
196196 }
197197
198+ private static string GetCustomProperty ( string ? item , System . Diagnostics . Activity activity )
199+ {
200+ if ( item is null || string . IsNullOrEmpty ( item ) )
201+ return string . Empty ;
202+
203+ return activity . GetCustomProperty ( item ) ? . ToString ( ) ?? activity . Parent ? . GetCustomProperty ( item ) ? . ToString ( ) ?? string . Empty ;
204+ }
205+
198206 private string GetDuration ( System . Diagnostics . Activity activity )
199207 {
200208 var duration = GetDurationTimeSpan ( activity ) ;
@@ -234,7 +242,7 @@ private string GetDuration(System.Diagnostics.Activity activity)
234242 return default ( TimeSpan ? ) ;
235243 }
236244
237- private static string GetCollectionItem < T > ( string item , IEnumerable < KeyValuePair < string , T > > collection ) where T : class
245+ private static string GetCollectionItem < T > ( string ? item , IEnumerable < KeyValuePair < string , T ? > > collection ) where T : class
238246 {
239247 if ( collection is ICollection < KeyValuePair < string , T > > emptyCollection && emptyCollection . Count == 0 )
240248 return string . Empty ; // Skip allocation of enumerator
@@ -243,14 +251,14 @@ private static string GetCollectionItem<T>(string item, IEnumerable<KeyValuePair
243251 {
244252 if ( string . CompareOrdinal ( keyValue . Key , item ) == 0 )
245253 {
246- return ConvertToString ( keyValue . Value ) ;
254+ return ConvertToString ( keyValue . Value ) ?? string . Empty ;
247255 }
248256 }
249257
250258 return string . Empty ; // Not found
251259 }
252260
253- private static void RenderStringDictionaryFlat < T > ( IEnumerable < KeyValuePair < string , T > > collection , StringBuilder builder ) where T : class
261+ private static void RenderStringDictionaryFlat < T > ( IEnumerable < KeyValuePair < string , T ? > > collection , StringBuilder builder ) where T : class
254262 {
255263 if ( collection is ICollection < KeyValuePair < string , T > > emptyCollection && emptyCollection . Count == 0 )
256264 return ; // Skip allocation of enumerator
@@ -263,16 +271,16 @@ private static void RenderStringDictionaryFlat<T>(IEnumerable<KeyValuePair<strin
263271 firstItem = false ;
264272 builder . Append ( keyValue . Key ) ;
265273
266- string stringValue = ConvertToString ( keyValue . Value ) ;
267- if ( stringValue != null )
268- {
269- builder . Append ( '=' ) ;
270- builder . Append ( stringValue ) ;
271- }
274+ var stringValue = ConvertToString ( keyValue . Value ) ;
275+ if ( stringValue is null )
276+ continue ;
277+
278+ builder . Append ( '=' ) ;
279+ builder . Append ( stringValue ) ;
272280 }
273281 }
274282
275- private static void RenderStringDictionaryJson < T > ( IEnumerable < KeyValuePair < string , T > > collection , StringBuilder builder , string dictionaryPrefix = "{ " ) where T : class
283+ private static void RenderStringDictionaryJson < T > ( IEnumerable < KeyValuePair < string , T ? > > collection , StringBuilder builder , string dictionaryPrefix = "{ " ) where T : class
276284 {
277285 if ( collection is ICollection < KeyValuePair < string , T > > emptyCollection && emptyCollection . Count == 0 )
278286 return ; // Skip allocation of enumerator
@@ -291,8 +299,8 @@ private static void RenderStringDictionaryJson<T>(IEnumerable<KeyValuePair<strin
291299 builder . Append ( '"' ) ;
292300 builder . Append ( EscapeStringQuotes ( keyValue . Key ) ) ;
293301
294- string stringValue = ConvertToString ( keyValue . Value ) ;
295- if ( stringValue == null )
302+ var stringValue = ConvertToString ( keyValue . Value ) ;
303+ if ( stringValue is null )
296304 builder . Append ( "\" : null" ) ;
297305 else
298306 builder . Append ( "\" : \" " ) . Append ( EscapeStringQuotes ( stringValue ) ) . Append ( '"' ) ;
@@ -346,7 +354,7 @@ private static void RenderActivityEventsJson(StringBuilder builder, IEnumerable<
346354 builder . Append ( " ]" ) ;
347355 }
348356
349- private string ConvertToString ( System . Diagnostics . ActivityTraceFlags traceFlags , string format )
357+ private string ConvertToString ( System . Diagnostics . ActivityTraceFlags traceFlags , string ? format )
350358 {
351359 if ( string . IsNullOrEmpty ( format ) )
352360 {
@@ -369,7 +377,7 @@ private string ConvertToString(System.Diagnostics.ActivityTraceFlags traceFlags,
369377 return traceFlags . ToString ( format ) ;
370378 }
371379
372- private static string ConvertToString ( System . Diagnostics . ActivityKind activityKind , string format )
380+ private static string ConvertToString ( System . Diagnostics . ActivityKind activityKind , string ? format )
373381 {
374382 if ( string . IsNullOrEmpty ( format ) )
375383 {
@@ -398,7 +406,7 @@ private static string ConvertToString(System.Diagnostics.ActivityKind activityKi
398406 return activityKind . ToString ( format ) ;
399407 }
400408
401- private string ConvertToString ( System . Diagnostics . ActivityStatusCode statusCode , string format )
409+ private string ConvertToString ( System . Diagnostics . ActivityStatusCode statusCode , string ? format )
402410 {
403411 if ( string . IsNullOrEmpty ( format ) )
404412 {
@@ -423,11 +431,11 @@ private string ConvertToString(System.Diagnostics.ActivityStatusCode statusCode,
423431 return statusCode . ToString ( format ) ;
424432 }
425433
426- private static string ConvertToString ( object objectValue )
434+ private static string ? ConvertToString ( object ? objectValue )
427435 {
428436 try
429437 {
430- if ( objectValue == null )
438+ if ( objectValue is null )
431439 return null ;
432440
433441 return Convert . ToString ( objectValue , System . Globalization . CultureInfo . InvariantCulture ) ;
@@ -438,7 +446,7 @@ private static string ConvertToString(object objectValue)
438446 }
439447 }
440448
441- private static bool FormatEnumAsInteger ( string format )
449+ private static bool FormatEnumAsInteger ( string ? format )
442450 {
443451 return format ? . Length == 1 && ( format [ 0 ] == 'd' || format [ 0 ] == 'D' ) ;
444452 }
0 commit comments