Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.TypeInitializationException in ActivityListener when accessing ActivitySource from a static field #110249

Closed
flibustier7seas opened this issue Nov 28, 2024 · 4 comments

Comments

@flibustier7seas
Copy link

Description

A System.TypeInitializationException may be thrown when an ActivityListener accesses an ActivitySource stored in a static field.

Reproduction Steps

[TestFixture]
public class DiagnosticsTests
{
    private static class Telemetry
    {
        public static ActivitySource MyActivitySource { get; } = new ActivitySource("MyActivitySource");
    }

    [Test]
    public void Test()
    {
        ActivitySource.AddActivityListener(new ActivityListener
        {
            ShouldListenTo = newActivitySource =>
            {
                var myActivitySource = Telemetry.MyActivitySource;

                // System.TypeInitializationException : The type initializer for 'Telemetry' threw an exception.
                //     ----> System.NullReferenceException : Object reference not set to an instance of an object.
                //     at DiagnosticsTests.Telemetry.get_MyActivitySource()
                return newActivitySource.Name == myActivitySource.Name;
            },
            Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData,
        });

        var activity = new ActivitySource("OtherActivitySource").StartActivity();

        Assert.That(activity, Is.Null);
    }
}

Expected behavior

Accessing ActivitySource within an ActivityListener should not result in an exception.

Actual behavior

Accessing ActivitySource within an ActivityListener causes a System.TypeInitializationException with an inner NullReferenceException.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

Issue for docs: dotnet/docs#42745

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 28, 2024
Copy link
Contributor

Tagging subscribers to this area: @tarekgh, @tommcdon, @pjanotti
See info in area-owners.md if you want to be subscribed.

@MichalPetryka
Copy link
Contributor

MichalPetryka commented Nov 28, 2024

The ActivitySource constructor is calling your ShouldListenTo before the field is set, seems like you can't access any ActivitySources inside of a listener.

@flibustier7seas
Copy link
Author

seems like you can't access any ActivitySources inside of a listener.

This behavior is not obvious; it might be a bug. If not, it should be documented properly.

@tarekgh
Copy link
Member

tarekgh commented Dec 2, 2024

This is not a bug. The order of the static constructor's initialization is not guaranteed and depends on the runtime. Also, we don't document every issue that can happen because of the order of the static initialization. In your code you can simply do:

            var myActivitySource = Telemetry.MyActivitySource;
            var activityListener = new ActivityListener
            {
                ShouldListenTo = newActivitySource => newActivitySource.Name == myActivitySource.Name,
                Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData,
            };
            ActivitySource.AddActivityListener(activityListener);

@tarekgh tarekgh closed this as not planned Won't fix, can't repro, duplicate, stale Dec 2, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants