Repository navigation
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:
cc Brian Robbins (@brianrob) Noah Falk (@noahfalk) Sourabh Shirhatti (@shirhatti)
Activity
brianrob commentedon Jan 14, 2020
David Fowler (@davidfowl), what mechanism (ETW, EventPipe, etc.) did you use to collect this? Also, can you share the trace out?
davidfowl commentedon Jan 14, 2020
Brian Robbins (@brianrob) dotnet trace. Sure
brianrob commentedon Jan 14, 2020
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.
davidfowl commentedon Jan 14, 2020
I was doing it on windows yes. I'll try ETW as well.
brianrob commentedon Jan 15, 2020
Thanks much.
davidfowl commentedon Jan 15, 2020
Exact same bug when using ETW and perf view to collect the trace
brianrob commentedon Jan 15, 2020
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.
davidfowl commentedon Jan 15, 2020
That's why I pointed to this source code:
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
Looks like when the string is null, something here is broken (or something lower in the stack).
sywhang commentedon Jan 16, 2020
I will take a look at this. Thanks
brianrob commentedon Jan 16, 2020
Thanks Sung Yoon Whang (@sywhang).
davidfowl commentedon Feb 6, 2020
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?
brianrob commentedon Feb 7, 2020
Yes, this should live in the dotnet/runtime repo.
sywhang commentedon Feb 10, 2020
David Fowler (@davidfowl) sorry I just got back from my 3-week vacation. I'll look at this some time this week.
sywhang commentedon Feb 12, 2020
The issue here is that the
EventDatabeing 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
WriteEventCoredirectly, since a payload size of 0 causes the entire parameter payload to be skipped. If you take a look atEventSource.WriteEvent, it converts all the null strings to empty strings for this reason:(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.
brianrob commentedon Feb 12, 2020
Thanks Sung Yoon Whang (@sywhang)!
sywhang commentedon Jun 26, 2020
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 :(
brianrob commentedon Feb 14, 2026
Fixed via dotnet/extensions#2957.