System.Diagnostics.ActivitySource.StartActivity returns 空

System.Diagnostics.ActivitySource.StartActivity returns null

我还没有找到使 activitySource.StartActivity return 非空 activity 的方法,这与 DiagnosticSource.StartActivity 行为不同。是预期的吗?我是否遗漏了一些明显的东西?

我可以看到 docs 说:“创建的 activity 对象,如果它有活动的侦听器,或者如果它没有事件侦听器则为 null。”下面的测试仍然失败,初始化ActivityListener的正确方法是什么?我使用的包是 "System.Diagnostics.DiagnosticSource" Version="5.0.0".

    [TestMethod]
    public void Start_Not_Null_When_ActivityListener_Added_And_ShouldListenTo_Explicitly_Defined_Activity()
    {
        var activitySource = new ActivitySource("ActivitySourceName");
        var activityListener = new ActivityListener
        {
            ShouldListenTo = s => true
        };
        ActivitySource.AddActivityListener(activityListener);
        
        using var activity = activitySource.StartActivity($"MethodType:/Path");
        
        Assert.IsNotNull(activity);
    }

github:

的帮助下通过了这个测试
[TestMethod]
public void Start_Not_Null_When_ActivityListener_Added_And_ShouldListenTo_Explicitly_Defined_Activity()
{
    var activitySource = new ActivitySource("ActivitySourceName");
    var activityListener = new ActivityListener
    {
        ShouldListenTo = s => true,
        SampleUsingParentId = (ref ActivityCreationOptions<string> activityOptions) => ActivitySamplingResult.AllData,
        Sample = (ref ActivityCreationOptions<ActivityContext> activityOptions) => ActivitySamplingResult.AllData
    };
    ActivitySource.AddActivityListener(activityListener);

    using var activity = activitySource.StartActivity("MethodType:/Path");

    Assert.IsNotNull(activity);
}