Skip to content

Bug in parsing logging events #1092

Closed
Closed

Description

I'm trying to use trace event to collect structured logs from a running process and I noticed that some data is being exposed from the wrong payload names.

As an example, here's an MVC project that logs using ILogger:

HasStack="True", 
ThreadID="18,028",
ProcessorNumber="0",
Level="2" 
FactoryID="1" 
LoggerName="Microsoft.AspNetCore.Hosting.Diagnostics" 
EventId="1" 
EventName="Request starting HTTP/1.1 GET http://localhost:7000/  " 
FormattedMessage=""
HasStack="True" 
ThreadID="18,028"
ProcessorNumber="0" 
Level="2" 
FactoryID="1" 
LoggerName="Microsoft.AspNetCore.Routing.EndpointMiddleware" 
EventId="0" 
EventName="ExecutingEndpoint" 
FormattedMessage="Executing endpoint '/Index'"
HasStack="True" 
ThreadID="18,028" 
ProcessorNumber="0" 
Level="2" 
FactoryID="1"
LoggerName="Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker"
EventId="3" 
EventName="PageExecuting" 
FormattedMessage="Route matched with {page = "/Index"}. Executing page /Index"

Notice that the first event has EventName as Request starting HTTP/1.1 GET http://localhost:7000/   and FormattedMessage as empty string. It seem as though there's an issue when event name is null or empty then the formatted message becomes the event name. This also reproduces with other logging events so I'd imagine it's the same bug.

Here's the code that produces one of the events:

https://github.com/dotnet/extensions/blob/dbf7b645e573670da7ba9567c41ec14298573260/src/Logging/Logging.EventSource/src/LoggingEventSource.cs#L128-L142

https://github.com/dotnet/extensions/blob/dbf7b645e573670da7ba9567c41ec14298573260/src/Logging/Logging.EventSource/src/LoggingEventSource.cs#L448-L475

cc Brian Robbins (@brianrob) Noah Falk (@noahfalk) Sourabh Shirhatti (@shirhatti)

Activity

  1. brianrob commented on Jan 14, 2020

    @brianrob
    Member

    David Fowler (@davidfowl), what mechanism (ETW, EventPipe, etc.) did you use to collect this? Also, can you share the trace out?

  2. davidfowl commented on Jan 14, 2020

    @davidfowl
    Author

    Brian Robbins (@brianrob) dotnet trace. Sure

  3. brianrob commented on Jan 14, 2020

    @brianrob
    Member

    Thanks David Fowler (@davidfowl). Out of curiosity, is this on Windows? It would be super valuable if you can try this with ETW and see if you get the same results. That would tell me if this is specific to EventPipe or if it's something agnostic on either side.

  4. davidfowl commented on Jan 14, 2020

    @davidfowl
    Author

    I was doing it on windows yes. I'll try ETW as well.

  5. brianrob commented on Jan 15, 2020

    @brianrob
    Member

    Thanks much.

  6. davidfowl commented on Jan 15, 2020

    @davidfowl
    Author

    Exact same bug when using ETW and perf view to collect the trace

  7. brianrob commented on Jan 15, 2020

    @brianrob
    Member

    Sung Yoon Whang (@sywhang) can you take a look at this? This appears to be an issue in how EventSource emits TraceLogging events specifically around NULL string payloads. David Fowler (@davidfowl) ran the scenario using both ETW and EventPipe with the same results. I also see the same results when opening the ETL in WPA, which implies that the issue is on the producer side. I can point you to the traces.

  8. sywhang commented on Jan 16, 2020

    @sywhang
    Contributor

    I will take a look at this. Thanks

  9. brianrob commented on Jan 16, 2020

    @brianrob
    Member
  10. davidfowl commented on Feb 6, 2020

    @davidfowl
    Author

    Sung Yoon Whang (@sywhang) any progress on this? I'd like to get it fixed in a patch of 3.1. Should we move it to dotnet/runtime or dotnet/aspnetcore?

  11. brianrob commented on Feb 7, 2020

    @brianrob
    Member

    Yes, this should live in the dotnet/runtime repo.

  12. sywhang commented on Feb 10, 2020

    @sywhang
    Contributor

    David Fowler (@davidfowl) sorry I just got back from my 3-week vacation. I'll look at this some time this week.

  13. sywhang commented on Feb 12, 2020

    @sywhang
    Contributor

    The issue here is that the EventData being written has size of 0 encoded for the string. This causes the entire parameter to not get copied to the buffer, and the parser (TraceEvent) tries to parse until the next null-terminated string, which just happens to be the next string (FormattedMessage).

    You shouldn't be writing null strings into WriteEventCore directly, since a payload size of 0 causes the entire parameter payload to be skipped. If you take a look at EventSource.WriteEvent, it converts all the null strings to empty strings for this reason:

            protected unsafe void WriteEvent(int eventId, string? arg1, string? arg2, string? arg3)
            {
                if (m_eventSourceEnabled)
                {
                    arg1 ??= "";
                    arg2 ??= "";
                    arg3 ??= "";
                    fixed (char* string1Bytes = arg1)
                    fixed (char* string2Bytes = arg2)
                    fixed (char* string3Bytes = arg3)
                    {
    
    

    (From https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs#L854)

    I filed dotnet/extensions#2957 to address this.

  14. brianrob commented on Feb 12, 2020

    @brianrob
    Member
  15. sywhang commented on Jun 26, 2020

    @sywhang
    Contributor

    This should be closed via dotnet/extensions#2957 - Brian Robbins (@brianrob) could you close this issue? For some reason it looks like I don't have access to that in this repo :(

  16. brianrob commented on Feb 14, 2026

    @brianrob
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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