I've got the following csharp program:
using System.Diagnostics;
using System.Runtime.InteropServices;
var listener = new ActivityListener()
{
// This is only for internal Sentry events
ShouldListenTo = (source) => source.Name.StartsWith("ConsoleApp9"),
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData,
ActivityStarted = activity => Console.WriteLine($"Started activity {activity.SpanId}"),
ActivityStopped = _ => { }
};
ActivitySource.AddActivityListener(listener);
var tracer = new ActivitySource("ConsoleApp9");
Console.WriteLine("Framework: {0}", RuntimeInformation.FrameworkDescription);
Console.WriteLine("Starting activity...");
var data = tracer.StartActivity("foo");
Console.WriteLine($"Stopping activity {data?.SpanId}");
data?.Stop();
So that it will run in .NET Framework, I've added an explicit reference to the System.Diagnostics.DiagnosticSource 8.0.0 NuGet package.
When I run this compile and run this using .NET 8.0 I'm seeing what I would expect:
Framework: .NET 8.0.2
Starting activity...
Started activity 07cf31b8b4e65543
Stopping activity 07cf31b8b4e65543
However when I compile this to .NET Framework 4.8 and run it using mono, none of the IDs are initialized, resulting in the following output:
Framework: Mono 6.12.0.199 (2020-02/f648ec1e0a6 Wed Jun 14 09:24:35 EDT 2023)
Starting activity...
Started activity 0000000000000000
Stopping activity 0000000000000000
Does anyone know what's causing this? Ultimately I'd like to have the IDs automatically generated on all target frameworks.
I got an answer from one of the contributors on the dotnet runtime:
Activity.SpanId gets a non-zero value only if the activity ID format is W3C. The default is W3C since .NET 5, but even in new versions of the System.Diagnostics.DiagnosticSource package, the new defaults only apply when your app is running on modern .NET, and it keeps using the older Hierarchical format on .NET Framework.
You can switch to the modern .NET defaults by writing Activity.DefaultIdFormat = ActivityIdFormat.W3C; at the start of your program.
There are also various other ways to work around this, described in the changelog for .net5.0