Why Langfuse drops .NET traces started inside ASP.NET Core requests
If your .NET service sends LLM generations to Langfuse over OpenTelemetry and only the calls made outside an HTTP request ever show up, look at Activity.Current. Inside a request it is ASP.NET Core's request activity, which exists but is not recorded, and an exporter that refuses activities with an unrecorded in-process parent will throw away every generation started under it. The fix is to clear Activity.Current before starting the Langfuse trace, so it becomes a real root, and restore the request activity when the trace is disposed.
What the symptom looked like
I had a .NET service that wraps each LLM call in a small disposable type: it starts a Langfuse trace, opens one generation inside it, records the model, input, output and token usage, and closes both on Dispose. The service also runs one generation at startup as an ingest check, to prove the exporter can reach Langfuse.
The startup check arrived every time. The generations made while serving real requests never did. There were no exporter errors, the HTTP responses were fine, and the code path that created the generation clearly ran. Langfuse simply never received anything from inside a request.
That asymmetry was the clue. The startup check runs with no ambient activity. A request does not.
Why an unrecorded parent hides the whole chain
In System.Diagnostics, an Activity can exist without being recorded. A listener's sampler decides, and ActivitySamplingResult.PropagationData means "create the activity so trace context flows, but don't collect data for it". Such an activity becomes Activity.Current like any other, but its Recorded flag is false and no exporter will ever see it.
ASP.NET Core starts an activity for each incoming request. In this service nothing asked to record it, so it sat at the top of every request as an unrecorded Activity.Current. Any activity started during the request took it as its in-process parent, including the Langfuse trace.
The exporter I was using walks the in-process parent chain and drops an activity if any ancestor is unrecorded. That is a defensible rule on its own: it avoids exporting orphaned children whose parent span will never exist in the backend. But it meant the trace and its generation were discarded at export time, quietly, for every request.
Why marking the trace as a root wasn't enough
The wrapper already created the trace with the library's "is root" option. That option gives the trace a fresh trace id, so in Langfuse it would look like a new trace. It does not change Activity.Parent. The .NET object still pointed at the request activity, and that in-process link is what the exporter checked.
There is a related trap in the plain ActivitySource API. Passing default(ActivityContext) as the parent to StartActivity does not mean "no parent". A default context means "use Activity.Current", so you get the ambient parent anyway. The only reliable way to start a true root in-process is to make Activity.Current null first.
How to detach the trace and put the request back
Activity.Current is settable and backed by an AsyncLocal. The fix is to remember the ambient activity, clear it, start the trace, and restore the ambient activity on Dispose, including on the paths where starting the trace fails. A minimal version looks like this:
using System.Diagnostics;
public sealed class DetachedTrace : IDisposable
{
private static readonly ActivitySource Source = new("Example.Llm");
private readonly Activity? ambient;
private readonly Activity? trace;
private DetachedTrace(Activity? ambient, Activity? trace)
{
this.ambient = ambient;
this.trace = trace;
}
// Synchronous on purpose: an AsyncLocal change made in an async method
// does not flow back to the caller.
public static DetachedTrace Start(string name)
{
Activity? ambient = Activity.Current;
Activity.Current = null;
try
{
return new DetachedTrace(ambient, Source.StartActivity(name));
}
catch
{
Activity.Current = ambient;
throw;
}
}
public void Dispose()
{
try
{
trace?.Dispose();
}
finally
{
Activity.Current = ambient;
}
}
}
Two details matter here. Start must be synchronous: a change to an AsyncLocal inside an async method stays inside that method's execution context and never reaches the caller, so the caller would still be running under the request activity. And Dispose restores the ambient activity in a finally, because the code after the LLM call (logging, the response, other instrumentation) should see the request activity exactly as before.
In my wrapper, telemetry failures are logged and swallowed so tracing can never fail the call it measures. With that design, the failure path still has to restore Activity.Current before returning the inert wrapper, or a broken exporter would leave the rest of the request with no ambient activity at all.
How to reproduce an unrecorded request activity in a unit test
You don't need a running web host to test this. An ActivityListener whose sampler returns PropagationData produces the same kind of activity ASP.NET Core leaves behind: created, current, not recorded.
[Fact]
public void Start_DetachesFromAnUnrecordedParent_AndDisposeRestoresIt()
{
using var requestSource = new ActivitySource($"test-{Guid.NewGuid():N}");
using var listener = new ActivityListener
{
ShouldListenTo = s => s == requestSource,
Sample = (ref ActivityCreationOptions<ActivityContext> _) =>
ActivitySamplingResult.PropagationData,
};
ActivitySource.AddActivityListener(listener);
using Activity request = requestSource.StartActivity("HttpRequestIn")!;
Assert.False(request.Recorded);
using (DetachedTrace.Start("llm-call"))
{
Assert.NotSame(request, Activity.Current);
}
Assert.Same(request, Activity.Current);
}
Use a unique source name per test. ActivitySource.AddActivityListener is process-wide, so tests running in parallel will see each other's activities unless something tells them apart. For the same reason, assert NotSame(request, ...) inside the block rather than Null: if another listener is attached to your trace's source, the new trace is legitimately current there.
A second test with a recording listener on the trace's source can then assert the useful property directly: the generation's parent is the trace, and the trace's Parent is null.
What you give up by detaching
A detached trace is no longer a child of the request in any other tracing backend. If you also export request spans somewhere else, the LLM call won't appear nested under them. If that correlation matters, carry it explicitly: put the request's trace id on the Langfuse trace as an attribute or metadata, or add an ActivityLink to the request's context when you start the trace. A link records the relationship without making the request the parent, so it doesn't trigger the unrecorded-parent rule.
For LLM observability I think detaching is the right default anyway. A Langfuse trace is usually one logical AI operation, and its natural boundary is the operation, not the HTTP request that happened to trigger it.
Key takeaways
- ASP.NET Core's request activity can be current without being recorded, and every activity started during the request inherits it as an in-process parent.
- An exporter that drops activities with an unrecorded ancestor will silently discard every Langfuse generation started inside a request, while startup work still gets through.
- Giving a trace a new trace id does not change
Activity.Parent, and passing a defaultActivityContexttoStartActivityfalls back toActivity.Current. - Clear
Activity.Currentin a synchronous start method, start the trace, and restore the ambient activity inDisposeand on every failure path. - An
ActivityListenerthat samples withPropagationDatareproduces an unrecorded request activity in a plain unit test.