Skip to content

EventListeners are never invoked in dotnet run app.cs #125336

@IanKemp

Description

@IanKemp

Description

When a dotnet run app.cs application that uses an EventListener is executed, nothing is ever recorded to that listener.

Reproduction Steps

Create a new C# "console app" solution using top-level statements. Replace the contents of Program.cs with the below. Build the solution so that an executable is generated.

using System.Collections.Concurrent;
using System.Diagnostics.Tracing;

using EventSourceEnumerator eventSourceEnumerator = new();

using HttpClient client = new();
await client.GetAsync("https://example.com");

Console.WriteLine($"Number of events recorded: {eventSourceEnumerator.Events.Count}");

// borrowed from https://www.meziantou.net/listing-all-available-etw-events-in-a-dotnet-application.htm
internal sealed class EventSourceEnumerator : EventListener
{
    public ConcurrentDictionary<string, ConcurrentDictionary<int, EventInfo>> Events { get; } = new();

    public EventSourceEnumerator()
    {
        EventSourceCreated += OnEventSourceCreated;
        EventWritten += OnEventWritten;
    }

    private void OnEventWritten(object? sender, EventWrittenEventArgs e)
    {
        var events = Events[e.EventSource.Name];
        if (events != null)
        {
            if (!events.ContainsKey(e.EventId))
            {
                events.TryAdd(e.EventId, new EventInfo(e.EventId, e.EventName, e.Level, e.PayloadNames?.ToArray() ?? Array.Empty<string>()));
            }
        }
    }

    private void OnEventSourceCreated(object? sender, EventSourceCreatedEventArgs e)
    {
        if (e.EventSource is null)
            return;

        if (!Events.ContainsKey(e.EventSource.Name))
        {
            EnableEvents(e.EventSource, EventLevel.LogAlways);
            Events.TryAdd(e.EventSource.Name, new ConcurrentDictionary<int, EventInfo>());
        }
    }
}

internal sealed record EventInfo(int Id, string? Name, EventLevel Level, string[] PayloadNames);

Expected behavior

Output should be

Number of events recorded: 15

in both cases.

Actual behavior

Correct:

> path\to\solution\directory\bin\Debug\net10.0\Program.exe
Number of events recorded: 15

Incorrect:

> dotnet run --file path\to\solution\directory\Program.cs
Number of events recorded: 0

Regression?

N/A, dotnet run app.cs is new in .NET 10 which is current at the time of writing.

Known Workarounds

None.

Configuration

> dotnet --info
.NET SDK:
 Version:           10.0.103
 Commit:            c2435c3e0f
 Workload version:  10.0.100-manifests.c992be6d
 MSBuild version:   18.0.11+c2435c3e0

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.26100
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\10.0.103\

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions