Skip to content

Replace IncludeActivity with typed Layout #52

@snakefoot

Description

@snakefoot

Maybe do this instead:

       public Layout<System.Diagnostics.ActivityTraceId?> TraceId { get; set; } = Layout<System.Diagnostics.ActivityTraceId?>.FromMethod(evt => System.Diagnostics.Activity.Current?.TraceId);

        public Layout<System.Diagnostics.ActivitySpanId?> SpanId { get; set; } = Layout<System.Diagnostics.ActivitySpanId?>.FromMethod(evt => System.Diagnostics.Activity.Current?.SpanId);
        private void AddActivityIfEnabled(ITelemetry trace)
        {
            var traceId = RenderLogEvent(TraceId, logEvent);
            if (traceId.HasValue)
                trace.Context.Operation.Id = traceId.Value.ToHexString();

            var spanId = RenderLogEvent(SpanId, logEvent);
            if (spanId.HasValue)
                trace.Context.Operation.ParentId = spanId.Value.ToHexString();
        }

If not wanting SpanId / TraceId, then one can "just" assign the values to empty-string (translates to null):

        <target xsi:type="ApplicationInsightsTarget" name="aiTarget" spanId="" traceId="">
            <connectionString>Your_ApplicationInsights_ConnectionString</connectionString>
            <contextproperty name="threadid" layout="${threadid}" />
        </target>

This will also work together with <targets async="true">

Notice using SpanId instead of ParentSpanId just like official ApplicationInsights:

                        if (currentActivity.IdFormat == ActivityIdFormat.W3C)
                        {
                            // Set OperationID to Activity.TraceId
                            itemOperationContext.Id = currentActivity.TraceId.ToHexString();

                            if (string.IsNullOrEmpty(itemOperationContext.ParentId))
                            {
                                itemOperationContext.ParentId = currentActivity.SpanId.ToHexString();
                            }
                        }

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions