@@ -57,13 +57,52 @@ public abstract class TelemetryConverterBase : ITelemetryConverter
5757
5858 static readonly MessageTemplateTextFormatter MessageTemplateTextFormatter = new ( "{Message:lj}" ) ;
5959
60+ private readonly bool _includeOperationIdPropertyAsTelemetryProperty ;
61+ private readonly bool _includeParentSpanIdPropertyAsTelemetryProperty ;
62+ private readonly bool _includeOperationNamePropertyAsTelemetryProperty ;
63+ private readonly bool _includeVersionPropertyAsTelemetryProperty ;
64+
6065 /// <summary>
6166 /// Creates an instance of <see cref="TelemetryConverterBase" /> using default value formatter (
6267 /// <see cref="ApplicationInsightsJsonValueFormatter" />).
6368 /// </summary>
6469 public TelemetryConverterBase ( )
70+ : this ( false , false , false , false )
71+ {
72+ }
73+
74+ /// <summary>
75+ /// Creates an instance of <see cref="TelemetryConverterBase" /> using default value formatter (
76+ /// <see cref="ApplicationInsightsJsonValueFormatter" />).
77+ /// </summary>
78+ /// <param name="includeOperationIdPropertyAsTelemetryProperty">
79+ /// if set to <c>true</c> the <see cref="OperationIdProperty" /> is added to the
80+ /// telemetry properties. Otherwise it is only set as <c>ITelemetry.Context.Operation.Id</c>.
81+ /// </param>
82+ /// <param name="includeParentSpanIdPropertyAsTelemetryProperty">
83+ /// if set to <c>true</c> the <see cref="ParentSpanIdProperty" /> is added to the
84+ /// telemetry properties. Otherwise it is only set as <c>ITelemetry.Context.Operation.ParentId</c>.
85+ /// </param>
86+ /// <param name="includeOperationNamePropertyAsTelemetryProperty">
87+ /// if set to <c>true</c> the <see cref="OperationNameProperty" /> is added to the
88+ /// telemetry properties. Otherwise it is only set as <c>ITelemetry.Context.Operation.Name</c>.
89+ /// </param>
90+ /// <param name="includeVersionPropertyAsTelemetryProperty">
91+ /// if set to <c>true</c> the <see cref="VersionProperty" /> is added to the
92+ /// telemetry properties. Otherwise it is only set as <c>ITelemetry.Context.Component.Version</c>.
93+ /// </param>
94+ public TelemetryConverterBase (
95+ bool includeOperationIdPropertyAsTelemetryProperty ,
96+ bool includeParentSpanIdPropertyAsTelemetryProperty ,
97+ bool includeOperationNamePropertyAsTelemetryProperty ,
98+ bool includeVersionPropertyAsTelemetryProperty )
6599 {
66100 ValueFormatter = new ApplicationInsightsJsonValueFormatter ( ) ;
101+
102+ _includeOperationIdPropertyAsTelemetryProperty = includeOperationIdPropertyAsTelemetryProperty ;
103+ _includeParentSpanIdPropertyAsTelemetryProperty = includeParentSpanIdPropertyAsTelemetryProperty ;
104+ _includeOperationNamePropertyAsTelemetryProperty = includeOperationNamePropertyAsTelemetryProperty ;
105+ _includeVersionPropertyAsTelemetryProperty = includeVersionPropertyAsTelemetryProperty ;
67106 }
68107
69108#pragma warning disable CS1591
@@ -155,42 +194,41 @@ public void ForwardPropertiesToTelemetryProperties(LogEvent logEvent,
155194
156195 if ( telemetryProperties is ITelemetry telemetry )
157196 {
158- // Operation.Id (TraceId)
159- if ( logEvent . Properties . TryGetValue ( OperationIdProperty , out var operationIdProp ) )
160- telemetry . Context . Operation . Id = operationIdProp . ToString ( ) . Trim ( '"' ) ;
161- else if ( logEvent . TraceId is ActivityTraceId traceId )
162- telemetry . Context . Operation . Id = traceId . ToHexString ( ) ;
163-
164- // Operation.ParentId (ParentSpanId)
165- if ( logEvent . Properties . TryGetValue ( ParentSpanIdProperty , out var parentSpanIdProp ) )
166- telemetry . Context . Operation . ParentId = parentSpanIdProp . ToString ( ) . Trim ( '"' ) ;
167-
168- // Operation.Name (OperationName)
169- if ( logEvent . Properties . TryGetValue ( OperationNameProperty , out var operationNameProp ) )
170- telemetry . Context . Operation . Name = operationNameProp . ToString ( ) . Trim ( '"' ) ;
171-
172- // Set Id for RequestTelemetry and DependencyTelemetry
173- if ( logEvent . SpanId is ActivitySpanId spanId )
174- {
175- if ( telemetry is RequestTelemetry req )
176- req . Id = spanId . ToHexString ( ) ;
177- else if ( telemetry is DependencyTelemetry dep )
178- dep . Id = spanId . ToHexString ( ) ;
179- }
197+ PopulateTelemetryFromLogEvent ( logEvent , telemetry ) ;
180198
181199 if ( telemetry . Context ? . Component != null
182200 && logEvent . Properties . TryGetValue ( VersionProperty , out var version ) )
183201 telemetry . Context . Component . Version = version . ToString ( ) . Trim ( '\" ' ) ;
184202 }
185203
186204 var baggageWasForwarded = ForwardActivityBaggage ( logEvent , telemetryProperties , formatProvider ) ;
205+ ForwardSimpleProperties ( logEvent , telemetryProperties , baggageWasForwarded ) ;
206+ }
187207
188- var filteredProperties = logEvent . Properties . Where ( property =>
189- property . Value != null
190- && ! ( baggageWasForwarded && BaggageProperty . Equals ( property . Key , StringComparison . Ordinal ) )
191- && ! telemetryProperties . Properties . ContainsKey ( property . Key ) ) ;
192- foreach ( var property in filteredProperties )
193- ValueFormatter . Format ( property . Key , property . Value , telemetryProperties . Properties ) ;
208+ private static void PopulateTelemetryFromLogEvent ( LogEvent logEvent , ITelemetry telemetry )
209+ {
210+ // Operation.Id (TraceId)
211+ if ( logEvent . Properties . TryGetValue ( OperationIdProperty , out var operationIdProp ) )
212+ telemetry . Context . Operation . Id = operationIdProp . ToString ( ) . Trim ( '"' ) ;
213+ else if ( logEvent . TraceId is ActivityTraceId traceId )
214+ telemetry . Context . Operation . Id = traceId . ToHexString ( ) ;
215+
216+ // Operation.ParentId (ParentSpanId)
217+ if ( logEvent . Properties . TryGetValue ( ParentSpanIdProperty , out var parentSpanIdProp ) )
218+ telemetry . Context . Operation . ParentId = parentSpanIdProp . ToString ( ) . Trim ( '"' ) ;
219+
220+ // Operation.Name (OperationName)
221+ if ( logEvent . Properties . TryGetValue ( OperationNameProperty , out var operationNameProp ) )
222+ telemetry . Context . Operation . Name = operationNameProp . ToString ( ) . Trim ( '"' ) ;
223+
224+ // Set Id for RequestTelemetry and DependencyTelemetry
225+ if ( logEvent . SpanId is ActivitySpanId spanId )
226+ {
227+ if ( telemetry is RequestTelemetry req )
228+ req . Id = spanId . ToHexString ( ) ;
229+ else if ( telemetry is DependencyTelemetry dep )
230+ dep . Id = spanId . ToHexString ( ) ;
231+ }
194232 }
195233
196234 private static bool ForwardActivityBaggage ( LogEvent logEvent , ISupportProperties telemetryProperties , IFormatProvider formatProvider )
@@ -216,6 +254,27 @@ private static bool ForwardActivityBaggage(LogEvent logEvent, ISupportProperties
216254 return true ;
217255 }
218256
257+ private void ForwardSimpleProperties ( LogEvent logEvent , ISupportProperties telemetryProperties , bool skipBaggage )
258+ {
259+ var skipOperationId = ! _includeOperationIdPropertyAsTelemetryProperty ;
260+ var skipParentSpanId = ! _includeParentSpanIdPropertyAsTelemetryProperty ;
261+ var skipOperationName = ! _includeOperationNamePropertyAsTelemetryProperty ;
262+ var skipVersion = ! _includeVersionPropertyAsTelemetryProperty ;
263+
264+ foreach ( var property in logEvent . Properties )
265+ {
266+ if ( property . Value is null ) continue ;
267+ if ( skipOperationId && OperationIdProperty . Equals ( property . Key , StringComparison . Ordinal ) ) continue ;
268+ if ( skipParentSpanId && ParentSpanIdProperty . Equals ( property . Key , StringComparison . Ordinal ) ) continue ;
269+ if ( skipOperationName && OperationNameProperty . Equals ( property . Key , StringComparison . Ordinal ) ) continue ;
270+ if ( skipVersion && VersionProperty . Equals ( property . Key , StringComparison . Ordinal ) ) continue ;
271+ if ( skipBaggage && BaggageProperty . Equals ( property . Key , StringComparison . Ordinal ) ) continue ;
272+ if ( telemetryProperties . Properties . ContainsKey ( property . Key ) ) continue ;
273+
274+ ValueFormatter . Format ( property . Key , property . Value , telemetryProperties . Properties ) ;
275+ }
276+ }
277+
219278 /// <summary>
220279 /// To the severity level.
221280 /// </summary>
0 commit comments