@@ -61,13 +61,14 @@ public abstract class TelemetryConverterBase : ITelemetryConverter
6161 readonly bool _includeParentSpanIdPropertyAsTelemetryProperty ;
6262 readonly bool _includeOperationNamePropertyAsTelemetryProperty ;
6363 readonly bool _includeVersionPropertyAsTelemetryProperty ;
64+ readonly bool _ignorePropertyNameCase ;
6465
6566 /// <summary>
6667 /// Creates an instance of <see cref="TelemetryConverterBase" /> using default value formatter (
6768 /// <see cref="ApplicationInsightsJsonValueFormatter" />).
6869 /// </summary>
6970 public TelemetryConverterBase ( )
70- : this ( false , false , false , false )
71+ : this ( false , false , false , false , true )
7172 {
7273 }
7374
@@ -91,18 +92,29 @@ public TelemetryConverterBase()
9192 /// if set to <c>true</c> the <see cref="VersionProperty" /> is added to the
9293 /// telemetry properties. Otherwise it is only set as <c>ITelemetry.Context.Component.Version</c>.
9394 /// </param>
95+ /// <param name="ignorePropertyNameCase">
96+ /// <para>
97+ /// if set to <c>true</c> property name lookups are case insensitive.
98+ /// </para>
99+ /// <para>
100+ /// The main use case set it to <c>true</c> for maximum compatibility with various logging frameworks
101+ /// but it has a performance impact when there are many properties on the <see cref="LogEvent" />.
102+ /// </para>
103+ /// </param>
94104 public TelemetryConverterBase (
95105 bool includeOperationIdPropertyAsTelemetryProperty ,
96106 bool includeParentSpanIdPropertyAsTelemetryProperty ,
97107 bool includeOperationNamePropertyAsTelemetryProperty ,
98- bool includeVersionPropertyAsTelemetryProperty )
108+ bool includeVersionPropertyAsTelemetryProperty ,
109+ bool ignorePropertyNameCase )
99110 {
100111 ValueFormatter = new ApplicationInsightsJsonValueFormatter ( ) ;
101112
102113 _includeOperationIdPropertyAsTelemetryProperty = includeOperationIdPropertyAsTelemetryProperty ;
103114 _includeParentSpanIdPropertyAsTelemetryProperty = includeParentSpanIdPropertyAsTelemetryProperty ;
104115 _includeOperationNamePropertyAsTelemetryProperty = includeOperationNamePropertyAsTelemetryProperty ;
105116 _includeVersionPropertyAsTelemetryProperty = includeVersionPropertyAsTelemetryProperty ;
117+ _ignorePropertyNameCase = ignorePropertyNameCase ;
106118 }
107119
108120#pragma warning disable CS1591
@@ -193,33 +205,27 @@ public void ForwardPropertiesToTelemetryProperties(LogEvent logEvent,
193205 telemetryProperties . Properties . Add ( TelemetryPropertiesMessageTemplate , logEvent . MessageTemplate . Text ) ;
194206
195207 if ( telemetryProperties is ITelemetry telemetry )
196- {
197208 PopulateTelemetryFromLogEvent ( logEvent , telemetry ) ;
198209
199- if ( telemetry . Context ? . Component != null
200- && logEvent . Properties . TryGetValue ( VersionProperty , out var version ) )
201- telemetry . Context . Component . Version = version . ToString ( ) . Trim ( '\" ' ) ;
202- }
203-
204210 var baggageWasForwarded = ForwardActivityBaggage ( logEvent , telemetryProperties , formatProvider ) ;
205211 ForwardSimpleProperties ( logEvent , telemetryProperties , baggageWasForwarded ) ;
206212 }
207213
208- private static void PopulateTelemetryFromLogEvent ( LogEvent logEvent , ITelemetry telemetry )
214+ private void PopulateTelemetryFromLogEvent ( LogEvent logEvent , ITelemetry telemetry )
209215 {
210216 // Operation.Id (TraceId)
211- if ( TrySetOperationIdFromLogEvent ( logEvent , telemetry , out var operationId ) )
217+ if ( TryGetOperationIdFromLogEvent ( logEvent , out var operationId ) )
212218 telemetry . Context . Operation . Id = operationId ;
213219 else if ( logEvent . TraceId is ActivityTraceId traceId )
214220 telemetry . Context . Operation . Id = traceId . ToHexString ( ) ;
215221
216222 // Operation.ParentId (ParentSpanId)
217- if ( logEvent . Properties . TryGetValue ( ParentSpanIdProperty , out var parentSpanIdProp ) )
218- telemetry . Context . Operation . ParentId = parentSpanIdProp . ToString ( ) . Trim ( '"' ) ;
223+ if ( TryGetParentSpanIdFromLogEvent ( logEvent , out var parentSpanId ) )
224+ telemetry . Context . Operation . ParentId = parentSpanId ;
219225
220226 // Operation.Name (OperationName)
221- if ( logEvent . Properties . TryGetValue ( OperationNameProperty , out var operationNameProp ) )
222- telemetry . Context . Operation . Name = operationNameProp . ToString ( ) . Trim ( '"' ) ;
227+ if ( TryGetOperationNameFromLogEvent ( logEvent , out var operationName ) )
228+ telemetry . Context . Operation . Name = operationName ;
223229
224230 // Set Id for RequestTelemetry and DependencyTelemetry
225231 if ( logEvent . SpanId is ActivitySpanId spanId )
@@ -229,31 +235,51 @@ private static void PopulateTelemetryFromLogEvent(LogEvent logEvent, ITelemetry
229235 else if ( telemetry is DependencyTelemetry dep )
230236 dep . Id = spanId . ToHexString ( ) ;
231237 }
238+
239+ if ( telemetry . Context ? . Component != null
240+ && TryGetVersionFromLogEvent ( logEvent , out var version ) )
241+ telemetry . Context . Component . Version = version ;
232242 }
233243
234- private static bool TrySetOperationIdFromLogEvent ( LogEvent logEvent , ITelemetry telemetry , out string operationId )
244+ private bool TryGetOperationIdFromLogEvent ( LogEvent logEvent , out string operationId )
245+ => TryGetPropertyFromLogEventIgnoreCase ( logEvent , OperationIdProperty , out operationId ) ;
246+
247+ private bool TryGetParentSpanIdFromLogEvent ( LogEvent logEvent , out string operationId )
248+ => TryGetPropertyFromLogEventIgnoreCase ( logEvent , ParentSpanIdProperty , out operationId ) ;
249+
250+ private bool TryGetOperationNameFromLogEvent ( LogEvent logEvent , out string operationId )
251+ => TryGetPropertyFromLogEventIgnoreCase ( logEvent , OperationNameProperty , out operationId ) ;
252+
253+ private bool TryGetVersionFromLogEvent ( LogEvent logEvent , out string version )
254+ => TryGetPropertyFromLogEventIgnoreCase ( logEvent , VersionProperty , out version ) ;
255+
256+ private bool TryGetPropertyFromLogEventIgnoreCase ( LogEvent logEvent , string propertyName , out string value )
235257 {
236- operationId = null ;
237- if ( logEvent . Properties . TryGetValue ( OperationIdProperty , out var operationIdProp ) )
258+ value = null ;
259+ if ( _ignorePropertyNameCase )
260+ {
261+ value = logEvent . Properties
262+ . FirstOrDefault ( p => string . Equals ( p . Key , propertyName , StringComparison . OrdinalIgnoreCase ) )
263+ . Value ?
264+ . ToString ( ) ;
265+ }
266+ else if ( logEvent . Properties . TryGetValue ( propertyName , out var operationIdProp ) )
238267 {
239- operationId = operationIdProp . ToString ( ) ;
268+ value = operationIdProp . ToString ( ) ;
240269 }
241270 else
242271 {
243- operationId = logEvent . Properties
244- . FirstOrDefault ( p => string . Equals ( p . Key , OperationIdProperty , StringComparison . OrdinalIgnoreCase ) )
245- . Value ?
246- . ToString ( ) ;
272+ return false ;
247273 }
248274
249- if ( string . IsNullOrEmpty ( operationId ) )
275+ if ( string . IsNullOrEmpty ( value ) )
250276 return false ;
251277
252- operationId = operationId . Trim ( '\" ' ) ;
278+ value = value . Trim ( '\" ' ) ;
253279 return true ;
254280 }
255281
256- private static bool ForwardActivityBaggage ( LogEvent logEvent , ISupportProperties telemetryProperties , IFormatProvider formatProvider )
282+ private bool ForwardActivityBaggage ( LogEvent logEvent , ISupportProperties telemetryProperties , IFormatProvider formatProvider )
257283 {
258284 if ( ! logEvent . Properties . TryGetValue ( BaggageProperty , out var baggageProp )
259285 || baggageProp is not StructureValue baggageStructure )
@@ -282,15 +308,16 @@ private void ForwardSimpleProperties(LogEvent logEvent, ISupportProperties telem
282308 var skipParentSpanId = ! _includeParentSpanIdPropertyAsTelemetryProperty ;
283309 var skipOperationName = ! _includeOperationNamePropertyAsTelemetryProperty ;
284310 var skipVersion = ! _includeVersionPropertyAsTelemetryProperty ;
311+ var stringComparison = _ignorePropertyNameCase ? StringComparison . OrdinalIgnoreCase : StringComparison . Ordinal ;
285312
286313 foreach ( var property in logEvent . Properties )
287314 {
288315 if ( property . Value is null ) continue ;
289- if ( skipOperationId && OperationIdProperty . Equals ( property . Key , StringComparison . Ordinal ) ) continue ;
290- if ( skipParentSpanId && ParentSpanIdProperty . Equals ( property . Key , StringComparison . Ordinal ) ) continue ;
291- if ( skipOperationName && OperationNameProperty . Equals ( property . Key , StringComparison . Ordinal ) ) continue ;
292- if ( skipVersion && VersionProperty . Equals ( property . Key , StringComparison . Ordinal ) ) continue ;
293- if ( skipBaggage && BaggageProperty . Equals ( property . Key , StringComparison . Ordinal ) ) continue ;
316+ if ( skipOperationId && OperationIdProperty . Equals ( property . Key , stringComparison ) ) continue ;
317+ if ( skipParentSpanId && ParentSpanIdProperty . Equals ( property . Key , stringComparison ) ) continue ;
318+ if ( skipOperationName && OperationNameProperty . Equals ( property . Key , stringComparison ) ) continue ;
319+ if ( skipVersion && VersionProperty . Equals ( property . Key , stringComparison ) ) continue ;
320+ if ( skipBaggage && BaggageProperty . Equals ( property . Key , stringComparison ) ) continue ;
294321 if ( telemetryProperties . Properties . ContainsKey ( property . Key ) ) continue ;
295322
296323 ValueFormatter . Format ( property . Key , property . Value , telemetryProperties . Properties ) ;
0 commit comments