-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Log machine readable & queryable structured log entries #6096
Comments
@gdoron can you explain a little more about your change. The logging passes all the information to the logger as the |
@rowanmiller The loggers by convention are looking for placeholders in the logged string to map it to a dictionary that can be later queried:
https://docs.asp.net/en/latest/fundamentals/logging.html Problem is, EF doesn't utilize this pattern but log human readable flat log entries but not machine readable structured log entries. I would love to create a PR for this one, but I didn't manage to add the source code of EF to my application in order to test and fiddle with my changes (compilation errors). Is there a little guide on how to do this? Anyway, you can add serilog and a sink, Seq is probably the easiest one to install but there are many others, change the message to include placeholders and see the difference for yourself. |
Hi folks, I had a quick look at the current logging statement, @rowanmiller I think the missing detail is that The suggestion of @gdoron's to use a template-based format string would resolve this nicely - MVC uses this approach: Making Let me know if I can help or provide more info. |
@nblumhardt , @gdoron @rowanmiller - Can you provide info on how to verify if the output of log is structured? |
@smitpatel using the built-in Microsoft.Extensions.Logging framework, you can plug in a mock The The event data stored this way should be logger.LogInformation("Hello, {Name}", "World"); So, assuming you've rigged up the logger to store data in var state = _logger.Events.Last();
var props = (IEnumerable<KeyValuePair<string,object>>)state;
var name = props.Single(kvp => kvp.Key == "Name");
Assert.Equal("World", name); All pseudocode but hopefully gives you some ideas. Let me know if you need more details. (To visually determine if the log is structured BTW, you can fire up Seq and see the captured properties highlighted pale yellow within the message, as per @gdoron's screenshots above.) HTH! |
Thanks @nblumhardt ! 👏 Would it be possible to squeeze it into v1.0.1? |
Hi @rowanmiller,
Currently EF logs raw strings without leveraging ASP.NET logging extensions.
source code
Filtering based on that string is very difficult if not impossible.
I wanted to create a pull request with a fix, I tried to compile the repository for hours but there were mismatch of assemblies I couldn't resolve for some reason.
Anyway the fix seems to be as simple as changing the code to:
And change
RelationalStrings.RelationalLoggerExecutedCommand
to:(Or simply use
LoggerMessage.Define
as MVC is doing)Which will yield a queryable log entry.
The first is EF log entry and the second one is using my code (not exactly as above since I couldn't compile the source code).
Now it is possible to query for all the queries which lasted more than x ms.
Can it please be fixed before 1.1.0?
Thanks
p.s. Please change from
elapsed
toElapsedMilliseconds
to be in sync with MVCThe text was updated successfully, but these errors were encountered: