Skip to content

Conversation

@hangy
Copy link
Contributor

@hangy hangy commented Dec 3, 2025

By adding support for OperationName property from serilog-tracing, I think this should fix #236

Copy link
Contributor

@nblumhardt nblumhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking a look!

I think this could work, but only if an enricher (ILogEventEnricher) is used to attach the OperationName property from Activity.Current - nothing else will currently set OperationName.

An enricher could be avoided by setting the operation name in the sink's Emit(LogEvent) method using ConditionalWeakTable, but this would require internally wrapping Serilog's batched sink implementation via LoggerSinkConfiguration.Wrap() so that the Emit() call is the one that happens on the application thread at the point the event is created, not the asynchronous/background thread EmitBatch..() methods.

@hangy hangy force-pushed the serilog-tracing-support branch from 9994dc5 to c00d3f9 Compare December 5, 2025 19:05
@hangy
Copy link
Contributor Author

hangy commented Dec 5, 2025

I think this could work, but only if an enricher (ILogEventEnricher) is used to attach the OperationName property from Activity.Current - nothing else will currently set OperationName.

An enricher could be avoided by setting the operation name in the sink's Emit(LogEvent) method using ConditionalWeakTable, but this would require internally wrapping Serilog's batched sink implementation via LoggerSinkConfiguration.Wrap() so that the Emit() call is the one that happens on the application thread at the point the event is created, not the asynchronous/background thread EmitBatch..() methods.

You're right. I'll take a look at it. I guess there are several camps when using this Sink. Personally, I don't really need the OperationName as we integrate this Sink with Traces being sent through the AppInsights SDK as well as Logs via Serilog. Will see how to best add the OperationName without disruping other usage scenarios.

return loggerConfiguration.Sink(new ApplicationInsightsSink(telemetryClient, telemetryConverter),
restrictedToMinimumLevel, levelSwitch);
}
var wrapper = LoggerSinkConfiguration.Wrap(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nblumhardt Is this the correct usage of the wrapper, as you suggested? It seems to work in this project's unit tests (Activity.Current is available), but this test setup seems to circumvent batching, anyways.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @hangy, I had this wrong, assuming the App Insights sink was batched using the newer mechanism. No wrapping is needed - the sink already implements ILogEventSink.Emit() so the change can be made there directly and this can be reverted.

logEvent.AddOrUpdateProperty(
new LogEventProperty(
TelemetryConverterBase.OperationNameProperty,
new ScalarValue(activity.OperationName)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately sinks can't/shouldn't enrich events on the way through, as they run in parallel and this can cause inconsistent results/threading issues for other sinks. This was what I had in mind when referring to ConditionalWeakTable, which can be used to attach properties to an object behind-the-scenes using a GC-friendly CLR trick.

@nblumhardt
Copy link
Contributor

Looking at #239 and the complexity involved here, I wonder if we're better off putting the new behavior added in 4.1 behind an opt-in configuration option? Currently it seems like fixing this reliably will take a while, so having the sink ignore LogEvent.TraceId etc. by default could provide a way to keep the existing ecosystem functioning while the dust settles?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Operation name is missing in traces during message handling after update from version 4.0.0 to 4.1.0

2 participants